Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Websockets

I'm trying to use two websockets on one page. This is my code:

var pageViewWs = new WebSocket("ws://localhost:9002/pageView");
var sessionWs = new WebSocket("ws://localhost:9002/session");

pageViewWs.onmessage = function (event) {
  alert("PageView");
};

sessionWs.onmessage = function (event) {
  alert("Session"); 
};

Only the PageView alert appears. On the server side no requests are made to /session, only to /pageView.

Now, if I switch var pageViewWs and var sessionWs around then the Session alert is shown instead of the PageView. It is not because they are alerts, I've tried appending to the body and to divs and I've stepped through using Firebug. It seems that only one WebSocket can be created at a time although in Firebug the properties for pageViewWs and sessionWs appear the same with the exception of their url.

I've only tested this in Firefox 15.0.1. Is there some sort of Websocket limitation whereby you can only run one at a time? Or is something wrong with my code?

like image 385
Arthur Avatar asked Sep 19 '12 15:09

Arthur


People also ask

Can you have multiple WebSockets?

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 you have?

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

How many WebSockets can a browser handle?

Now Chrome allows 255 websocket connections.

What will replace WebSockets?

WebTransport is a new specification offering an alternative to WebSockets. For applications that need low-latency, event-driven communication between endpoints, WebSockets has been the go-to choice, but WebTransport may change that.


1 Answers

I believe you can only create one WebSocket connection from a client to a specific port on the host. Have you tried either running the two services on different ports, or on different servers? This would allow you to determine the limitation...

like image 64
logical Chimp Avatar answered Oct 01 '22 23:10

logical Chimp