Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

585
Views
Connect to Socket.IO server with specific path and namespace

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'});
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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'});
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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');
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!