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

419
Visualizações
How to create multiple Nodejs socket io server-client?

I am new to Nodejs and Socket.io, and this is first time when I am creating any chat application, so pardon me if I am asking some silly question.

In my web I have to kind of chat services, one is Live debate which is kind of chat room, and another one is private messaging like Facebook Messenger.

I have created both, in private messenger before showing the message I am checking the conversation Id, it's working quite appropriately. Live debate is also working appropriately.

But there is a issue, any message sent in private messenger displays in live debate window also. So I change the Server.js file for messenger and also changed the listening port, now the listening port for live debate is 3000 and for messenger is 8050, but still Live debate receiving the messenger messages.

Am I doing this in wrong way? Is there any other way to run two chat applications ?

I am using this server code

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');

server.listen(3000);
io.on('connection', function (socket) {

  console.log("client connected");
  var redisClient = redis.createClient();
  redisClient.subscribe('message');

  redisClient.on("message", function(channel, data) {
    console.log("mew message add in queue "+ data+ " channel");
    socket.emit(channel, data);
  });

  socket.on('disconnect', function() {
    redisClient.quit();
  });

});

With using Namespace server.js

var nsp = io.of('/debate')
nsp.on('connection', function (socket) {

  console.log("client connected"+socket.id);
  var redisClient = redis.createClient();
  redisClient.subscribe('message');
  var redisClient1 = redis.createClient();
  redisClient1.subscribe('debate');

  redisClient.on("message", function(channel, data) {
    console.log("mew message add in queue "+ data+ " channel");
    nsp.emit(channel, data);

  });   

  socket.on('disconnect', function() {
    redisClient.quit();
  });

});

client code

var socket = io.connect('http://localhost:3000/debate');
socket.on('message', function (data) {
    data = jQuery.parseJSON(data);
    console.log(data.user);
    $( "#messages" ).append( "<strong>"+data.user+":</strong><p>"+data.message+"</p>" );
    $('#messages').animate({
    scrollTop: $('#messages').get(0).scrollHeight}, 200);
});
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

socket.io supports the use of different namespaces. You should use that feature instead of creating two individual servers. After that you can use socket.emit to that specific namespace. For more information see the documentation: https://socket.io/docs/rooms-and-namespaces/

over 4 years ago · Santiago Trujillo Relatório

0

It's not too difficult, I wrote a chat app, as I think everyone does when they start nodejs, but nodeJS has rooms which are quite easy to use.

io.on('connection', function(client){

    client.on('room_connection', function(id){
        client.join(id); // User joins room.
    })

   client.on('message', function(data){
       io.to(data.room).emit('message', {message:data.message, client:client.conn.id});
   });
})

This is pretty much all you need. This works for PM's since u simply won't allow multiple users to join this chatroom, and normal chatrooms which multiple users can join.

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