I'm working on a single page Backbone application that is going to use web sockets. The application is fairly complex, with 6 or more major areas (screens).
The syntax of web sockets seems straightforward enough and I'm wondering now about the architecture.
When using web sockets, is it most performant to take the first approach or the 2nd approach?
1: Open a single websocket for all live server communication, on any screen or area of the application, and then filter those messages on the client side?
or...
2: Open multiple websockets at a time, where each web socket represents some area of functionality in the application
(I've seen this page, but it's about the server side and I'm interested in the client side: What is the best practice for WebSocket server(s)?)
Update: server is using Jetty (a Java technology not unlike Tomcat).
By default, a single server can handle 65,536 socket connections just because it's the max number of TCP ports available.
A server can open WebSocket connections with multiple clients—even multiple connections with the same client. It can then message one, some, or all of these clients. Practically, this means multiple people can connect to our chat app, and we can message some of them at a time.
The infamous 6 connections per host limit does not apply to WebSockets. Instead a far bigger limit holds (255 in Chrome and 200 in Firefox). This blessing is also a curse. It means that end users opening lots of tabs can cause large amounts of load and consume large amounts of continuous server resources.
In order to communicate using the WebSocket protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server. The URL to which to connect; this should be the URL to which the WebSocket server will respond.
I would open only one connection, easier to manage. To ease debugging you could namespace all your events with the area name. Also socket.io supports namespaces, see section "Restricting yourself to a namespace" here http://socket.io/#how-to-use.
Also if your doing lot of communications with your websocket you can turn it off with the Visibility API, example here: https://developer.mozilla.org/en-US/docs/DOM/Using_the_Page_Visibility_API
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With