20

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?

3
  • 2
    Does jsfiddle.net/DuQx9/1 work for you?
    – apsillers
    Commented Sep 19, 2012 at 16:12
  • apsillers, yes that does work for me.
    – Arthur
    Commented Sep 19, 2012 at 16:15
  • There might be a client-side bug in your JS, but it might also be a server-side bug or configuration issue. What language/server are you using?
    – apsillers
    Commented Sep 19, 2012 at 16:29

3 Answers 3

3

I faced the same problem to run multiple services through the same port. So, I created a PHP library to do this.

Why ?

Some free plans of hosting providers don't allow you to bind to ports or allow you to bind to one port. In case of OpenShift Cloud Server, you can only bind to port 8080. So, running multiple WebSocket services is not possible. In this case, Francium DiffSocket is useful.

You can run different services on the same port using a PHP library called Francium DiffSocket.

After setting up Francium DiffSocket, you can do this for using different services :

var chatWS = new WebSocket("ws://ws.example.com:8000/?service=chat");
var gameWS = new WebSocket("ws://ws.example.com:8000/?service=game");

An example are these services which are running through a single port :

2
  • @vulkan I pulled the demo servers down because the free tier expired. The library still works though.
    – Subin
    Commented Jul 26, 2019 at 9:38
  • demo works but doesn't work when I add it with composer.
    – vciloglu
    Commented Jul 30, 2019 at 9:00
2

Apart from the HTTP Request head both the request are the same. They hit the same application server on the same port. It is up to the server side application to treat each connection differently based on the HTTP request that initiated it.

I've done this in node. You could do it manually but packages like

eases the process.

1

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

5
  • 4
    I just tried using two different ports and it worked! Very interesting; I wonder why that is the case.
    – Arthur
    Commented Sep 19, 2012 at 16:11
  • 9
    iirc you're opening a connection to the specific port, and keeping it open - therefor it doesn't make sense to open a second connection to the same port (the traffic should just travel through the first connection) Commented Sep 19, 2012 at 16:15
  • 2
    Any other js calls that are made from the page to server such as AJAX POSTs will also be using that port. It handles passing those through at the same time so I thought that it would handle multiple WS
    – Arthur
    Commented Sep 19, 2012 at 16:17
  • 9
    Multiple connections to the same port should not be an issue. See How do multiple clients connect simultaneously to one port...?: [I]f I connect to the same web server twice from my client, the two connections will have different source ports from my perspective and destination ports from the web server's. So there is no ambiguity, even though both connections have the same source and destination IP addresses.
    – apsillers
    Commented Sep 19, 2012 at 16:27
  • 5
    So, to emphasize: multiple WebSocket connections to one server port is totally valid. It's possible that some browsers may introduce limitations for this, but it will never be an inherent limitation to WebSockets, and so it will depend on the runtime/browser you're using whether you can have multiple connections. Commented Dec 16, 2017 at 18:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.