Daniel Gruszczyk
Daniel Gruszczyk

Reputation: 5612

Socket.io client ignoring port when namespace used [Bug?]

I have a simple node.js app with socket.io (1.3.5), taken from socket.io examples:

// Setup basic express server
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 3000;

server.listen(port, function () {
  console.log('Server listening at port %d', port);
});

// Routing
app.use(express.static(__dirname + '/public'));

io.of('/admin').on('connection', function(socket){
    //handle conection on /admin namespace
});

io.of('/user').on('connection', function(socket){
    //handle conection on /user namespace
});

Now in my front-end I connect to these specific namespaces like so (again, taken from the example):

var admin_socket = io('/admin');
var user_socket = io('/user');

The app is running on port 3000 and the website is opened using URL localhost:3000.
When doing that I am getting CORS errors, it seems like Socket.io on client side is not auto-detecting the port number as soon as I start using namespaces (in firefox dev tools I can see requests going to localhost/ rather than localhost:3000/).


If on my server-side I don't use namespaces:

io.on('connection', function(socket){
    //handle general conection
});

And on front-end I connect this way:

var socket = io();

Everything works fine, port auto-discovery works and in firefox dev tools I can see connections being made to localhost:3000/.


Alternatively, if I still use namespaces on my back-end, and on front end I connect like so:

var admin_socket = io('localhost:3000/admin');
var user_socket = io(':3000/user');   //I can skip localhost

Again everything works (and indeed in firefox dev tools I can see network requests going to localhost:3000/).


How come the port auto-discovery is not working with namespaces? Is there a way to get it to work? Am I missing something here? Thanks.


See my answer below for a fix...

Upvotes: 9

Views: 2351

Answers (3)

Daniel Gruszczyk
Daniel Gruszczyk

Reputation: 5612

Ok so I did some debugging of code in socket.io.js and realized there is a potential bug there. On line 1050 a loc.hostname is used instead of loc.host. This causes a hostname to be used when passing in a namespace, this doesn't include port number.
In case of no namespace being used, on line 1024 loc.host is being used and everything is fine.
I have taken a copy of the file and changed line 1050 to use host and everything works fine.
Found github issue with that, it is fixed in 1.4.x: https://github.com/Automattic/socket.io-client/issues/812

Upvotes: 1

Stilldabomb
Stilldabomb

Reputation: 3

I don't think there is any way to get the auto port discovery to work without modifying the actual Socket.io code or waiting for a fix. The simplest thing you could do is just insert the current location.port including a colon before your namespace.

var admin_socket = io(':' + location.port + '/admin');
var user_socket = io(':' + location.port + '/user');

Or create a new function that will create a socket for you.

function sio(nsp) {
    return io(':' + location.port + nsp);
}

Upvotes: 0

kaytrance
kaytrance

Reputation: 2757

No need to mess with ports, it pretty much should work just by

var admin_socket = io('/admin');
var user_socket = io('/user');

Upvotes: 0

Related Questions