Jacksonkr
Jacksonkr

Reputation: 32207

NodesJS SocketIO - How can I make sure websockets are being used?

Background

To connect to my websocket from Unity I'm using ws://localhost:3000/socket.io/?EIO=4&transport=websocket. If I hit that link via browser I see the following:

{"code":3,"message":"Bad request"}

At this point I forced websockets for my NodeJS application with io.set('transports', ['websocket']); but then my application stopped working.

Question

How can I make sure websockets are available for NodeJS+SocketIO?

Code

app.js

var app = require('express')();
var http = require('http').Server(app);

app.get('/', function(req, res){
        res.sendFile(__dirname + '/index.html');
});

var server = app.listen(3000, function () {
   var host = server.address().address
   var port = server.address().port
});
var io = require('socket.io').listen(server);
//io.set('transports', ['websocket']);

...

Setup

Upvotes: 1

Views: 4389

Answers (2)

jfriend00
jfriend00

Reputation: 707158

By default, a socket.io client starts out with http polling and then switches to webSocket after a few polling transactions. As best I've been able to tell, nothing you can do on the server will change the way the client behaves so if you don't support polling on your server and you don't change the way the client is configured, then the initial socket.io client connection will not work. But, you can make corresponding changes in BOTH client and server to force socket.io to only use webSocket, even from the beginning. If you're looking at the network trace, you will still see an initial HTTP connection because all webSocket connections start with an HTTP connection, but then that HTTP connection is "upgraded" to the webSocket protocol.

You can see all the details on how to configure both client and server to ONLY use webSocket here: Socket.io 1.x: use WebSockets only?. In a nutshell, you will want to do this in the client for making a connection:

var socket = io({transports: ['websocket'], upgrade: false});

Upvotes: 6

rsp
rsp

Reputation: 111278

You seem to be using both WebSocket and Socket.io and it can cause conflicts. You should choose one and stick to it. If you're sure that your client will use WebSocket then there is no need to use Socket.io at all. Just use WebSocket ad you will have to worry about the AJAX, comet, long-polling workarounds.

This is an example server-side code using WebSocket:

var path = require('path');
var app = require('express')();
var ws = require('express-ws')(app);
app.get('/', (req, res) => {
  console.error('express connection');
  res.sendFile(path.join(__dirname, 'ws.html'));
});
app.ws('/', (s, req) => {
  console.error('websocket connection');
  for (var t = 0; t < 3; t++)
    setTimeout(() => s.send('message from server', ()=>{}), 1000*t);
});
app.listen(3001, () => console.error('listening on http://localhost:3001/'));
console.error('websocket example');

As you can see this is pretty simple.

See code examples in those answers for more info:

Upvotes: 4

Related Questions