15

I've read pretty much every guide and tutorial I can find on WebSockets, but not one of them has covered how to use them efficiently.

Does anyone have any sort of guide on how to do this? I'm concerned about the amount of bandwidth a single connection can take up, especially when applications have hundrends of connections opened.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Alexander
  • 5,661
  • 6
  • 24
  • 19
  • Actually I think the host will do pretty well to keep those puppies in line, or the ISP will. – jcolebrand Jun 01 '11 at 19:31
  • 1
    Probably, but a little optimization can't hurt, especially in applications where network latency is a huge factor. – Alexander Jun 01 '11 at 19:34
  • 2
    So you want QoS for websockets ... or what exactly? A global maximum of them? Are you worried about the client or the server here? – jcolebrand Jun 01 '11 at 19:36
  • Things like optimizing the connection to send as much information as possible, using a minimal amount of bandwidth, and things like that. – Alexander Jun 01 '11 at 19:41
  • No, I just want general advice and techniques on getting the most out of each connection, really. – Alexander Jun 01 '11 at 20:08
  • Use JSON. I use it in my project to differentiate between commands and all kinds of data easily with a minimum amount of bandwidth. – pimvdb Jun 01 '11 at 20:10
  • 1
    @Alexander: The efficiency depends on the architecture of the server e.g. threading. As application developer you only send as little as possible. – Jonas Jun 01 '11 at 20:11
  • @Jonas: The server I've been using is Node.js with the websocket-server module. I eventually to move over to a Python-based implementation, if ever I find a decent one (that has proper documentation. So far, no luck), but I have no idea what that means in terms of server architecture. \ – Alexander Jun 01 '11 at 20:20
  • @pimvdb: I've been using JSON, but I was hoping to find a more efficient format. – Alexander Jun 01 '11 at 20:21
  • 1
    @Alexander use [Bison](https://github.com/BonsaiDen/BiSON.js) – Raynos Jun 01 '11 at 20:35
  • 1
    @Alexander: The Node.js you are using is quite efficient. – Jonas Jun 01 '11 at 20:55
  • 1
    You shouldn't be concerned with the "amount of bandwidth a single connection can take up", generally the issue with larger public facing websites using WebSockets is the amount of concurrent connections active. Mitigating such a problem is usually dealt with by distributing connections among multiple web-farm nodes through a high performance load-balancing router – Kind Contributor Jan 19 '13 at 00:36

1 Answers1

27

The efficiency in WebSocket depends on the implementation and architecture of the webserver that handles them. WebSocket is a very efficient protocol with only 2 byte (!) overhead, compare to the all different HTTP header fields and HTTP cookies that is sent in every Ajax request.

Usually an efficient webserver for websockets should be event driven (see the examples below). In opposite, the traditional way to implement web servers have been to spawn a new thread per request. But threads allocate much memory e.g. 256MB per thread. So if you have 1GB memory on your server you can't handle very many requests concurrently. But if your server is event-driven your memory usage will be almost constant, since new threads aren't created. See also Technically why is processes in Erlang more efficient than OS threads?

You can send any data in your WebSockets. JSON is efficient if the client is a web browser, or maybe typed arrays. If your client is a custom application, you can use Protocol Buffers. See also Google Wave Client-Server Protocol Whitepaper

You can implement efficient websocket servers using Twisted (Python) or Netty (Java). Play Framework is an efficient web framework for Java and Scala implemented on Netty. Another efficient alternative is Yaws web server (Erlang) or Node.js + Socket.io (JavaScript).

As an application developer, the less data you send the more efficient (less traffic) and the less load on your server.

Community
  • 1
  • 1
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 2
    Can you expand on what circumstances a thread might use up 256MB? Is this something to do with WebSocket technology in particular? – Harry Johnston Dec 28 '16 at 05:14
  • 2
    Thread memory depends on OS and technology. In general, a thread for a JVM on 64-bit byte system 1024k of memory, not 256 MB. A thread is a light-weight process, not the entire OS. – user3000951 Dec 28 '16 at 03:46