Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are WebSockets really meant to be handled by Web servers?

Tags:

websocket

The WebSocket standard hasn't been ratified yet, however from the draft it appears that the technology is meant to be implemented in Web servers. pywebsocket implements a WebSocket server which can be dedicated or loaded as Apache plugin.

So what I am am wondering is: what's the ideal use of WebSockets? Does it make any sense to implement a service using as dedicated WebSocket servers or is it better to rethink it to run on top of WebSocket-enabled Web server?

like image 206
tunnuz Avatar asked Nov 21 '11 16:11

tunnuz


People also ask

Does WebSocket require web server?

By definition websockets like normal sockets are client-server so yes, you need a server.

What is the underlying communication protocol that WebSockets use?

The WebSocket protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request. By default the WebSocket protocol uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over TLS [RFC2818].

Can server initiate WebSocket connection?

No. The server can't initiate a connection.

How do WebSockets work internally?

WebSocket uses HTTP as the initial transport mechanism, but keeps the TCP connection alive after the HTTP response is received so that it can be used for sending messages between client and server. WebSockets allow us to build “real-time” applications without the use of long-polling.


1 Answers

The WebSocket protocol was designed with three models in mind:

  • A WebSocket server running completely separately from any web server.
  • A WebSocket server running separately from a web server, but with traffic proxied to the websocket server from the web server (allowing websocket and HTTP traffic to co-exist on the same port)
  • A WebSocket server running as a plugin in the web server.

The model you pick really depends on the application you are trying to build and some other constraints that may limit your choices.

For example, if your application is going to be served from a single web server and the WebSocket connection will always be back to that same server, then it probably makes sense to just run the WebSocket server as a plugin/module in the web server.

On the other hand if you have a general WebSocket service that is usable from many different web sites (for example, you could have continuous low-latency traffic updates served from a WebSocket server), then you probably want to run the WebSocket server separate from any web server.

Basically, the tighter the integration between your WebSocket service and your web service, the more likely you will want to run them together and on the same port.

There are some constraints that may force one model or another:

  • If you control the server(s) but not the incoming firewall rules, then you probably have no choice but to run the WebSocket server on the same port(s) as your HTTP/HTTPS server (e.g. 80 and 443). In which case you will have to use a web server plugin or proxy to the real WebSocket server.
  • On the other hand, if you do not have super-user permission on the server where you are running the WebSocket server, then you will probably not be able to use ports 80 and 443 (below 1024 is generally a privileged port range) and in that case it really doesn't matter whether you run the HTTP/S and WebSocket servers on the same port or not.
  • If you have cookie based authentication (such as OAuth) in the web server and you would like to re-use this for the WebSocket connections then you will probably want to run them together (special case of tight integration).
like image 165
kanaka Avatar answered Nov 01 '22 19:11

kanaka