70

Shall I use WebSocket on non-80 ports? Does it ruin the whole purpose of using existing web/HTTP infrastructures? And I think it no longer fits the name WebSocket on non-80 ports.

If I use WebSocket over other ports, why not just use TCP directly? Or is there any special benefits in the WebSocket protocol itself?

And since current WebSocket handshake is in the form of a HTTP UPGRADE request, does it mean I have to enable HTTP protocol on the port so that WebSocket handshake can be accomplished?

smwikipedia
  • 52,824
  • 76
  • 267
  • 432

3 Answers3

85

Shall I use WebSocket on non-80 ports? Does it ruin the whole purpose of using existing web/HTTP infrastructures? And I think it no longer fits the name WebSocket on non-80 ports.

You can run a webSocket server on any port that your host OS allows and that your client will be allowed to connect to.

However, there are a number of advantages to running it on port 80 (or 443).

  1. Networking infrastructure is generally already deployed and open on port 80 for outbound connections from the places that clients live (like desktop computers, mobile devices, etc...) to the places that servers live (like data centers). So, new holes in the firewall or router configurations, etc... are usually not required in order to deploy a webSocket app on port 80. Configuration changes may be required to run on different ports. For example, many large corporate networks are very picky about what ports outbound connections can be made on and are configured only for certain standard and expected behaviors. Picking a non-standard port for a webSocket connection may not be allowed from some corporate networks. This is the BIG reason to use port 80 (maximum interoperability from private networks that have locked down configurations).

  2. Many webSocket apps running from the browser wish to leverage existing security/login/auth infrastructure already being used on port 80 for the host web page. Using that exact same infrastructure to check authentication of a webSocket connection may be simpler if everything is on the same port.

  3. Some server infrastructures for webSockets (such as socket.io in node.js) use a combined server infrastructure (single process, one listener) to support both HTTP requests and webSockets. This is simpler if both are on the same port.


If I use WebSocket over other ports, why not just use TCP directly? Or is there any special benefits in the WebSocket protocol itself?

The webSocket protocol was originally defined to work from a browser to a server. There is no generic TCP access from a browser so if you want a persistent socket without custom browser add-ons, then a webSocket is what is offered. As compared to a plain TCP connection, the webSocket protocol offers the ability to leverage HTTP authentication and cookies, a standard way of doing app-level and end-to-end keep-alive ping/pong (TCP offers hop-level keep-alive, but not end-to-end), a built in framing protocol (you'd have to design your own packet formats in TCP) and a lot of libraries that support these higher level features. Basically, webSocket works at a higher level than TCP (using TCP under the covers) and offers more built-in features that most people find useful. For example, if using TCP, one of the first things you have to do is get or design a protocol (a means of expressing your data). This is already built-in with webSocket.

And since current WebSocket handshake is in the form of a HTTP UPGRADE request, does it mean I have to enable HTTP protocol on the port so that WebSocket handshake can be accomplished?

You MUST have an HTTP server running on the port that you wish to use webSocket on because all webSocket requests start with an HTTP request. It wouldn't have to be heavily featured HTTP server, but it does have to handle the initial HTTP request.

jfriend00
  • 580,699
  • 78
  • 809
  • 825
  • 1
    This answer provides a few invalid arguments. Cookies are never port-bound, they're sent to any port on the same host. And WebSockets are not subject to the Same-Origin-Policy, you explicitly have to check the `origin` header server-side to prevent cross-origin access. – kelunik Oct 25 '17 at 05:35
  • 1
    @kelunik - Corrections applied. I was wrong about the cookies so I've removed any reference to cookies. My notion of cross origin issues came from socket.io (built on top of webSocket) because socket.io (by default) starts with several http polling requests and then switches over to a webSocket and thus socket.io can hit the cross origin issue. But, since that doesn't apply to just a plain webSocket, I've removed that from the answer entirely. – jfriend00 Oct 25 '17 at 05:40
4

Yes - Use 443 (ie, the HTTPS port) instead.

There's little reason these days to use port 80 (HTTP) for anything other than a redirection to port 443 (HTTPS), as certification (via services like LetsEncrypt) are easy and free to set up.

The only possible exceptions to this rule are local development, and non-internet facing services.

Should I use a non-standard port?

I suspect this is the intent of your question. To this, I'd argue that doing so adds an unnecessary layer of complication with no obvious benefits. It doesn't add security, and it doesn't make anything easier.

But it does mean that specific firewall exceptions need to be made to host and connect to your websocket server. This means that people accessing your services from a corporate/school/locked down environment are probably not going to be able to use it, unless they can somehow convince management that it is mandatory. I doubt there are many good reasons to exclude your userbase in this way.

But there's nothing stopping you from doing it either...

Shadow
  • 6,976
  • 4
  • 39
  • 52
3

In my opinion, yes you can. 80 is the default port, but you can change it to any as you like.

Jeremy
  • 638
  • 1
  • 7
  • 17
  • 2
    Then why do we call it **Web**Socket? Because it use port 80 by default? Or because WebScoket handshake request is in HTTP format? I have this question because I take `Web` as synonymous to `HTTP`. – smwikipedia Feb 15 '15 at 04:55
  • 8
    I found it. From here https://developer.mozilla.org/en-US/docs/WebSockets/Writing_WebSocket_servers, it says: *The handshake is the "Web" in WebSockets. It's the bridge from HTTP to WS.*. – smwikipedia Feb 15 '15 at 05:10