43

My Node.js application is running at URL http://www.example.com/myapp/.

I have configured a Socket.IO server (version 1.3.5) with a custom namespace. Here is an example code snippet:

var server = http.createServer(...);
var io = socketio(server);
io
    .of('/a/b/c')
    .on('connection', function (socket) {
        socket.emit('update', {msg: '/a/b/c'});
    });

I can't figure out how to connect to this service from the client. My guesses (none of these is working):

io.connect('http://www.example.com/myapp/a/b/c');
io.connect('http://www.example.com', {path: '/myapp/a/b/c'});
io.connect('', {path: '/myapp/a/b/c'});
io.connect('http://www.example.com/a/b/c', {path: '/myapp'});
io.connect('http://www.example.com', {path: '/myapp/socket.io/a/b/c'});
guidoman
  • 1,071
  • 2
  • 11
  • 20

3 Answers3

89

On your server, don't forget to specify the path as well:

var io  = require('socket.io')(http, { path: '/myapp/socket.io'});

io
.of('/my-namespace')
.on('connection', function(socket){
    console.log('a user connected with id %s', socket.id);

    socket.on('my-message', function (data) {
        io.of('my-namespace').emit('my-message', data);
        // or socket.emit(...)
        console.log('broadcasting my-message', data);
    });
});

On your client, don't confuse namespace and path:

var socket = io('http://www.example.com/my-namespace', { path: '/myapp/socket.io'});
  • Web client side was just the following sufficient for me: var socket = io({ path: '/myapp/socket.io'}); In a react native project i used: var socket = io.connect(url, {path: path, transports: ['websocket']}); – WiRa Feb 09 '18 at 10:33
  • How to emit messages to a particular socket id with path ? . I implemented the same , but socket emits all the response to same user – Jayanth Suvarna Mar 23 '19 at 07:28
11

I'm using 1.3.5 too, in a slightly similar scenario, from an Angular single page app, where the client code for socket.io is just concatenated with the rest of the app (from a bower package), rather than downloaded/included from a particular network location.

What seems to work for me in the setup where my socket.io is at:

http://somedomain.com:9096/sockets/socket.io.js

rather than the default:

http://somedomain.com:9096/socket.io/socket.io.js

(I manually adjusted the path on the server side), is:

io.connect('http://somedomain.com:9096' + '/' + namespaceName, { path: '/sockets' });

It looks equivalent to your scenario:

io.connect('http://www.example.com/a/b/c', {path: '/myapp'});

which may be worth another try. I haven't fully tested the namespaceName with forward slashes in it, but it seems to pick up the connection on the client side, when I simply change my namespace to '/a/b/c'

What probably makes the difference is my server side setup, which goes:

var server = http.createServer(app);
var io = require('socket.io')(server, { path: '/sockets' }).listen(server);

My answer is more of a general indication that it is possible to use both a namespace and a customised path, despite the setup being unobvious. I hope it may be useful for you in some way.

Wojtek
  • 111
  • 1
  • 5
1

You can check the official documentation on Rooms and Namespaces. Basically, the great thing about socket.io is that, once your client requests the client-side sources, it will transmit all the necessary details required for the client to connect to the server (host, path, port, etc.).

To connect to your specific namespace, on the client you would simply have to specify:

var socket = io('/a/b/c');
Filip Dupanović
  • 32,650
  • 13
  • 84
  • 114
  • 4
    Apparently it doesn't work. It will try to connect to something like `http://www.example.com/socket.io/?EIO=3&transport=polling&t=1428495850033-1`, which of course returns `404 Not Found`. – guidoman Apr 08 '15 at 12:28
  • It looks like your HTTP server is correctly configured and that you were able to successfully get the `socket.io.js` client sources from the `socket.io` server handler. Well, that's weird that it won't work. I'll have to find some time and do a quick local setup to see how things fare on my end. – Filip Dupanović Apr 08 '15 at 12:47
  • 4
    I should also note that, if I configure the Socket.IO server without namespaces, i.e., without the `.of('/a/b/c')` stuff, the following client code works as expected: `io.connect('' , {path: '/myapp/socket.io'})`. – guidoman Apr 08 '15 at 12:56
  • @guidoman Hello, did you find any solution for your problem? I am experiencing same behaviour you stated above. – necilAlbayrak Feb 20 '19 at 10:39