Creating a private chat between a key using a node.js and socket.io

44,273

Solution 1

You have to create a room with conversation_id and make users to subscribe to that room, so that you can emit a private message to that room it by,

client

var socket = io.connect('http://ip:port');

socket.emit('subscribe', conversation_id);

socket.emit('send message', {
    room: conversation_id,
    message: "Some message"
});

socket.on('conversation private post', function(data) {
    //display data.message
});

Server

socket.on('subscribe', function(room) {
    console.log('joining room', room);
    socket.join(room);
});

socket.on('send message', function(data) {
    console.log('sending room post', data.room);
    socket.broadcast.to(data.room).emit('conversation private post', {
        message: data.message
    });
});

Here is the docs and example for creating a room, subscribing to the room and Emit message to a room:

  1. Socket.io Rooms
  2. Socket.IO subscribe to multiple channels
  3. Socket.io rooms difference between broadcast.to and sockets.in

Solution 2

SURE: Simply,

This is what you need :

 io.to(socket.id).emit("event", data);

whenever a user joined to the server,socket details will be generated including ID.This is the ID really helps to send a message to particular people.

first we need to store all the socket.ids in array,

   var people={};

   people[name] =  socket.id;

here name is the reciever name. Example:

  people["ccccc"]=2387423cjhgfwerwer23;

So, now we can get that socket.id with the reciever name whenever we are sending message:

for this we need to know the recievername.You need to emit reciever name to the server.

final thing is:

  socket.on('chat message', function(data){
 io.to(people[data.reciever]).emit('chat message', data.msg);
 });

Hope this works well for you.!!Good Luck

Share:
44,273

Related videos on Youtube

Asa Carter
Author by

Asa Carter

PHP developer, editor of web resources blog, Web Resource Source. Founder of Type Flex CMS. Shopify App Developer @ Appifiny Based in Scotland, UK.

Updated on July 15, 2022

Comments

  • Asa Carter
    Asa Carter almost 2 years

    How do I emit a message to all users in a private chat sharing a conversation_id using node.js and socket.io?

    var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);
    conversations = {};
    
    app.get('/', function(req, res) {
    res.sendfile('/');
    });
    
    io.sockets.on('connection', function (socket) {
    
    socket.on('send message', function (data) {
    
        var conversation_id = data.conversation_id;
    
        if (conversation_id in conversations) {
    
            console.log (conversation_id + ' is already in the conversations object');
    
            // emit the message [data.message] to all connected users in the conversation
    
        } else {
            socket.conversation_id = data;
            conversations[socket.conversation_id] = socket;
    
            conversations[conversation_id] = data.conversation_id;
    
            console.log ('adding '  + conversation_id + ' to conversations.');
    
            // emit the message [data.message] to all connected users in the conversation
    
        }
    })
    });
    
    server.listen(8080);
    
  • Asa Carter
    Asa Carter about 10 years
    Great, thanks, it works perfectly. I changed socket.broadcast.to() to io.sockets.in() so that it includes the emitting socket.
  • AbdulMomen عبدالمؤمن
    AbdulMomen عبدالمؤمن over 7 years
    this helped me a lot divillysausages.com/2015/07/12/an-intro-to-socket-io , see Sending a message to another user