Sending a specific socket.id data with node.js/socket.io

Recommended Posts

So I have my server running and I can't figure out how to send to the specific socket.ID. The code in dispatchGameMessage may look funny bcuz I been trying various things to send to the 2 people in the Game. Google isn't helping much with this. And now some code snipplets:

// varsvar express = require('express');var app = express();var server = require('http').createServer(app);var io = require('socket.io')(server);var port = 3000;  server.listen(port, function () {  console.log('Server listening at port %d', port);}); // Routingapp.use(express.static(__dirname));  // usernames which are currently connected to the chatvar i = 0;var channels = {};var channelRefs = {};var numUsers = 0;var clientHoles = new Array();var found = false; io.on('connection', function (socket) {    // when the client emits "C" - Create Channel/Join, this listens and executes  socket.on('C', function (data) {  console.log("Data Connect" + socket.id, port); found = false;//increase # of clients connected++numUsers; // we store the numUsers as socket session usernamesocket.username = data; addedUser = true; //first user connectedif(numUsers == 1){channels[data] = socket.id;socket.emit('W', "W");}else{//go through every connection availablefor(i = 1; i < numUsers; i++){console.log(channels[data], port);//if channel has a player in itif(channels.hasOwnProperty(data) && channels[data] != -1){socket.emit('W', socket.id);channelRefs[data] = {id1: channels[data], id2: socket.id};channels[data] = -1; //game in progress/finished//this may also be the problemdispatchGameMessage(data, "S", socket);found = true;break;}} //if channel not found, create itif(found == false){socket.emit('W', socket.id);channels[data] = socket.id;}}});    });});  //the problem is in here =]// Sends game command to channel (Game In Progress)function dispatchGameMessage (chan_name, data, sock) {//channelRefs[chan_name].id1 and channelRefs[chan_name].id2 are different if(data == "C"){console.log("ID 1 = " + channelRefs[chan_name].id1);console.log("ID 2 = " + channelRefs[chan_name].id2);}//bleeeeeeeeeeeeif(data.length == 1){//sock.broadcast(data, data);sock.socket(channelRefs[chan_name].id2).emit(data, channelRefs[chan_name].id2); sock.socket(socket(channelRefs[chan_name].id1)).emit(data, channelRefs[chan_name].id1); }}
No matter what I do, it sends the message be the client(socketID) that sent the message.

Share this post

Link to post
Share on other sites

I agree, rooms would be a more adequate solution.


Anyway, to send data to a specific socket:

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

The io.to() method is used to emit message to a room, and every socket is placed in a room named after its ID (which is documented on the link @bulkan provided).

Share this post

Link to post
Share on other sites

Yes, but it depends on your version of socket.io

With newer versions you can do


Where namespace is the namespace you are using, or '/' which is the default namespace, and room is the name of your room.

With older versions (when namespaces didn't exist) you'd do:


Share this post

Link to post
Share on other sites

Thanks @Gio


I guess I got one more question regarding the sockets for anyone who can answer.


Say I got 2 people in a room.


How can I get the socket.id of a certain position or index of a certain room.


Since my game only needs 2 players per room, when "player 1" sends a command, I would like to emit a message saying its "player 1" sending the command back to "game players" instead of there socket.id which I am currently using and checking if the socket id is them on client. Sending 1 or 2 which would represent the player would shorten the socket length when dispatching to room.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.