1

Background

  • I'm working to connect a Unity application to a SocketIO application
  • Unity is using a SocketIO Plugin (only uses websockets, not polling/xhr)

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

  • Ubuntu@14.04
  • NodeJS@0.10.25
  • SocketI@O1.7.3
  • express@4.15.2
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
  • why are you using ws and socket.io ? seems like you should only need one and if you want only websockets use ws ? – corn3lius Mar 28 '17 at 14:51

2 Answers2

6

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});
Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
4

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:

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
  • Nice list! I would agree except I had browsers in the past that didn't accept ws (iOs < v7) and the ability for socket.io to negotiate the transport and not bother how I implemented it was key. When he said only websockets I totally agree that socket.io isn't 'easier'. – corn3lius Mar 28 '17 at 15:07
  • even with the `--harmony` flag I'm having problems with getting this to run. Stuck on the `class` keyword `WebSocket.js:31` - do you use `babel-cli` for es2015? – Jacksonkr Mar 28 '17 at 21:15
  • 1
    @Jacksonkr If your Node doesn't support the `class` keyword then you must be using a very outdated and not supported version of Node, at least 0.12 or older. See http://node.green/#ES2015-functions-class The current version is 7.x and 8.0 will be released soon. – rsp Mar 29 '17 at 11:58
  • In order to get and updated node (currently 7.7.4) I had to `wget -qO- https://deb.nodesource.com/setup_7.x | sudo bash -` then `sudo apt-get install nodejs` – Jacksonkr Mar 29 '17 at 15:47