Trying to understand WebSocket URL schematic

So this might be noobish of me, but I’ll just ask anyways. So I’ve read up on WebSockets and how it works, however, I’m still trying to understand the setup of the schematic WebSocket URL. I know that you have to first call ws:// for HTTP or wss:// for HTTPS, then you have your website URL, then the port, then the URL to the script you are trying to run. However, I still don’t understand why the port is in there, is it required? Why can’t you just do something like http://localhost/the/path/to/the/file.php or https://localhost/the/path/to/the/next/file.asp?

I want to fully understand how WebSockets work before I get myself into a deep hole.

1 Like

A handy way to think about it is the URL is like the address of an apartment complex and the port is the apartment number you want to get to.

If you leave out the port number the default for HTTP requests is port 80. Try visiting http://sitepoint.com:80

The default for HTTPS is 443 - try visiting https://google.com:443

Ports allow many applications to receive and send requests on the same computer without using something like nginx to create a reverse proxy.

2 Likes

By the time you fully understand WebSockets (TCP) you will be in a deep hole

TCP - Transmission Control Protocol - is no easy thing to even begin to understand let alone fully understand.

That said, as jhliberty posted, the main take-away is ports

I think as long as you can manage to get a basic understanding and focus on using the API
on an as-needed basis you should be able to do OK.

1 Like

I feel like this applies to pretty much everything in programming and computers really… I think it’s the hardest part to convey to people sometimes. I have some friends that are very deep diver learners like I tend to be… but programming has been such a different experience learning than anything else I’ve learned. There are so many dependent areas of understanding on how all these things work and work together - the best way to learn things as much like @Mittineague said:

I see, I get what you guys are saying, but I should of made myself more clear. What I meant to ask was why have the port in the URL schema if you already have the HTTP request in the beginning? Say you want to use http, the port is normally at 80 so you would just do ws:// to connect to the server and the URL. But if you want to use SSL, you’d use wss:// and the server URL. However, why do you need to specify the port if say you’re already running wss:// for SSL and ws:// for regular HTTP request?

The question is why use both ws://localhost.com:80/the/path/to/the/file.html when ws:// already sends port 80? Or wss://localhost.com:443/the/path/to/the/file.html when wss:// already sends port 443.

Why is it that you have to repeat the same port twice? Or am I misunderstanding how the URL schema works?

If the websocket server is running on port 80 have you tried connecting to ws://localhost.com/the/path/to/the/file.html without the port? You may be answering your own question if it just works.

The reason ports are used is that often a single machine runs multiple servers. e.g.

In this example a standard http server runs on port 8080 and the websocket server runs on 8081.
Try downloading this example and changing the port to 80 and connecting without the port in client.js.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.