47

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'});
1
  • just use : after your base url then pass the path for eg:-- io('example.com:', {path: '/myapp/a/b/c'}); Commented Mar 21, 2022 at 13:59

3 Answers 3

97

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'});
Sign up to request clarification or add additional context in comments.

3 Comments

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']});
How to emit messages to a particular socket id with path ? . I implemented the same , but socket emits all the response to same user
This answer saved my life. I wasted hours misunderstanding path as namespace. Actually the both are different things. Thanks.
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.

Comments

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');

4 Comments

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.
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.
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 Hello, did you find any solution for your problem? I am experiencing same behaviour you stated above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.