Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

580
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda