Rewrite: To establish a connection with a Socket.IO server, specify the desired path and namespace.

86πŸ‘

When setting up your server, be sure to specify the path:

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);
        // Alternatively, you can use socket.emit(...)
        console.log('Broadcasting my-message', data);
      });
    });
  

When connecting from the client, make sure to differentiate between the namespace and the path:

var socket = io('http://www.example.com/my-namespace', { path: '/myapp/socket.io'});
  

86πŸ‘

When setting up your server, be sure to specify the path:

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);
        // Alternatively, you can use socket.emit(...)
        console.log('Broadcasting my-message', data);
      });
    });
  

When connecting from the client, make sure to differentiate between the namespace and the path:

var socket = io('http://www.example.com/my-namespace', { path: '/myapp/socket.io'});
  

11πŸ‘

I am also using version 1.3.5 in a slightly similar scenario. I am working on an Angular single page app where the client code for socket.io is concatenated with the rest of the app from a bower package. Instead of downloading or including the socket.io code from a specific network location, I have it stored at:

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

rather than the default:

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

I have adjusted the path manually on the server-side to match this setup. What seems to work for me is the following:

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

This seems equivalent to your scenario:

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

which you may want to try again. I haven’t fully tested using a 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'.

The server-side setup might be what makes the difference. In my case, I have the following setup:

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

My answer is just a general indication that it is possible to use both a namespace and a customized path, although the setup might not be obvious. I hope this information is useful to you in some way.

1πŸ‘

You can find the official documentation on Rooms and Namespaces here.

Socket.io provides a great feature called Rooms and Namespaces. When a client requests the client-side sources, it receives all the necessary details to connect to the server, such as the host, path, and port.

To connect to a specific namespace on the client-side, you need to specify the namespace as follows:

var socket = io('/a/b/c');

Read more interesting post

Leave a comment