29

I'm using Socket IO v1.4.5 and have tried 3 different ways below but dont have any result.

client.emit('test', 'hahahaha');
io.sockets.socket(id).emit('test',''hahaha); 
io.sockets.connected[id].emit('test','hahaha');

Here is my server side

var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var dateFormat = require('date-format');
var app = express();
var server = http.createServer( app );
var io = socket.listen( server );
io.sockets.on( 'connection', function( client ) {
    user[client.id]=client;

//when we receive message 
    client.on('message', function( data ) {
        console.log( 'Message received from' + data.name + ":" + data.message +' avatar' +data.avatar );
        client.emit('test', 'hahahaha');
});

Any help would be great.Thanks for help.Kind Regard

3 Answers3

127

To send a message to a specific client you need to do it like so:

socket.broadcast.to(socketid).emit('message', 'for your eyes only');

Here is a nice little cheat sheet for sockets:

 // sending to sender-client only
 socket.emit('message', "this is a test");

 // sending to all clients, include sender
 io.emit('message', "this is a test");

 // sending to all clients except sender
 socket.broadcast.emit('message', "this is a test");

 // sending to all clients in 'game' room(channel) except sender
 socket.broadcast.to('game').emit('message', 'nice game');

 // sending to all clients in 'game' room(channel), include sender
 io.in('game').emit('message', 'cool game');

 // sending to sender client, only if they are in 'game' room(channel)
 socket.to('game').emit('message', 'enjoy the game');

 // sending to all clients in namespace 'myNamespace', include sender
 io.of('myNamespace').emit('message', 'gg');

 // sending to individual socketid
 socket.broadcast.to(socketid).emit('message', 'for your eyes only');

Credit to https://stackoverflow.com/a/10099325


The easiest way rather than sending directly to the socket, would be creating a room for the 2 users to use and just send messages freely in there.

socket.join('some-unique-room-name'); // Do this for both users you want to chat with each other
socket.broadcast.to('the-unique-room-name').emit('message', 'blah'); // Send a message to the chat room.

Otherwise, you're going to need to keep track of each individual clients socket connection, and when you want to chat you'll have to look up that sockets connection and emit specifically to that one using the function I said above. Rooms are probably easier.

Community
  • 1
  • 1
Datsik
  • 14,453
  • 14
  • 80
  • 121
  • Hi.So i just need add that line to the server site? And does anything else look fine? –  Feb 28 '16 at 10:12
  • 1
    @HoàngPhúcVũ I'm not 100% sure what you're trying to accomplish in your code, you say you want to send to a specific socket connection, but I see no attempt at looking for a socket id and sending to them. – Datsik Feb 28 '16 at 10:16
  • What I want is send private message between 2 user.So do you have any suggest about this case.thanks for help –  Feb 28 '16 at 10:18
  • @Datsik hi, could you yo helo me on this topic? http://stackoverflow.com/questions/38817680/nodejs-could-not-detect-online-soket – mahdi pishguy Aug 07 '16 at 19:56
  • 3
    **Please note**: if the socket that uses the `socket.broadcast.to(id)` is oneself (hence, `socket.id == id`) then this won't work. Instead, use `io.sockets.to(id).emit('event-name-message', 'this is only for you')`. – Fed Jan 18 '18 at 13:00
0

Socket.io Version 2.0.3+

Sending a message to a specific socket

    let namespace = null;
    let ns = _io.of(namespace || "/");
    let socket = ns.connected[socketId] // assuming you have  id of the socket
    if (socket) {
        console.log("Socket Connected, sent through socket");
        socket.emit("chatMessage", data);
    } else {
        console.log("Socket not connected, sending through push notification");
    }
partikles
  • 331
  • 3
  • 13
-3

Simply do this

io.socket.in('room').emit("send message to everyone", data);
Muhammad Awais
  • 156
  • 1
  • 6