12

I need a server to be able to accept connections from both websocket and socket.io clients, is that possible? When I'm running just socket.io, also socket.io client works fine, but standard websocket client is unable to connect. On the other hand when I run websocket server alongside socket.io, websocket worsk well, but in browser, which is trying to connect via socket.io, I see error WebSocket connection to ... failed: Invalid frame header. Is it possible to get both connections working on single server instance?

I'm using express.io and websocket-node and it would be great to get it working only with express.oi.

1
  • Why do you need to do this? socket.io is an additional protocol on top of webSocket so you can't use the same server-side code to listen to both a plain webSocket and a socket.io connection. It would be easiest to put one of the two connections on a different port and just use a different handler for each, letting a standard library for each handle what they were built for. Commented Apr 13, 2015 at 21:31

2 Answers 2

21

While @jfriend00 is right that socket.io is an additional protocol on top of webSocket, I found the additional protocol might be quite simple. I think [email protected] server implicitly runs a WebSocket server powered by [email protected], I managed to (with the help of F12/network tool) connect to it with a native WebSocket client.

socket.io client:

var socket = io('http://localhost');
socket.emit('hello', 'there');

websocket client:

var ws = new WebSocket('ws://localhost/socket.io/?EIO=3&transport=websocket');
ws.send('42' + JSON.stringify(['hello', 'there']));
// ws.onmessage will get a MessageEvent object with the data property being encoded in the similar way.

Socket.IO API is more abstract, it deals with event and args, while WebSocket API deals with string.

Sign up to request clarification or add additional context in comments.

7 Comments

What's up with ws.send('42'...? Is the 42 needed for any reason?
@Mobilpadde Yes, I think the number is some kind of message type. If I remember right, there is at least one another number, for "probe" message.
@Mobilpadde You can see it yourself: open socket.io/demos/chat in Chrome, in F12 > network, filter with "EIO=3&transport=websocket", there should be some of them, and the response headers should contain "Connection:upgrade", and there should be "Frames" tab in detail panel, instead of "Preview" and "Response" tabs for normal HTTP requests. The contents listed in "Frames" tab are quite intuitive, such as '2probe', '42["login", {numUsers: 10}]'. I think what browsers support here is websocket, i.e., the upgraded HTTP, not Socket.IO.
42 is the answer to everything
- 4 being Engine.IO "message" packet type - 2 being Socket.IO "message" packet type socket.io/docs/v4/#is-socketio-still-needed-today
|
1

If anyone is facing issue when connecting to a socket.io server which runs version 4.x.x (i.e. engine.io version 4), refer to the following discussion of socket.io github repo:

https://github.com/socketio/socket.io/discussions/4299

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.