Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a single-page web application keep one Web Socket connection to the server or several?

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).

like image 509
SimplGy Avatar asked Nov 08 '12 19:11

SimplGy


People also ask

How many WebSockets can a server handle?

By default, a single server can handle 65,536 socket connections just because it's the max number of TCP ports available.

Can a client have multiple WebSocket connections?

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.

How many WebSockets can connect to a browser?

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.

How do I manage my WebSocket connections?

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.


1 Answers

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

like image 156
julesbou Avatar answered Sep 30 '22 23:09

julesbou