0

How would one send a message out to a single socket using socket.io.

I was thinking something like this but it doesn't work.

io.sockets.socket(socket.id).emit('messagetype', "message")

I get this error:

TypeError: Property 'connected' of object #<Namespace> is not a function

I think I'm just having an issue with syntax but can't find a source about it.

1
  • 1
    If you already have the socket, as in socket.id, why not just do socket.emit('messagetype', "message") ? Commented Dec 26, 2014 at 9:46

2 Answers 2

1

in your code you are usind to feth other persons socket but (socket.id) will always return your own socket id so you have to save others socket id in your database or in any array then you can send them any message:

   io.sockets.socket(socket_array['socketid']).emit('','');

here socket_array is an array where you have to save others socket id and 'socketid' is the key to feth the socketid;you can use username of others in stead of 'socketid', then it would be an unique id.

Sign up to request clarification or add additional context in comments.

Comments

1

Basically this is what you need.

io.on('connection', function(socket){
  socket.emit('messagetype', "message");
});

It's well documented at socketio-docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.