Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending message to specific client with socket.io and empty message queue

I´m going crazy with socket.io! Documentation is so bad it's simply not true.

I want to send a feedback to specific client over socket.io

My server side looks like this:

app.get('/upload', requiresLogin, function(request, response) {     response.render('upload/index.jade');     io.sockets.on('connection', function (socket) {         console.log('SOCKET ID ' + socket.id);         io.sockets.socket(socket.id).emit('new', 'hello');     }); }); 

and the client side looks like this:

$(document).ready(function() {     var socket = io.connect('http://localhost:80/socket.io/socket.io.js');     socket.on('new', function (data) {          console.log(socket.id);         console.log(data);          //$('#state').html(data.status);     }); }); 

but the client does simply nothing. I have tried nearly everything. Can someone tell me what I am doing wrong, please! :(

like image 556
krevativ Avatar asked Aug 02 '11 14:08

krevativ


People also ask

How do you send a message to a specific person using socket IO?

You can try the code below: 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.

How do I send a message to a socket?

The send() function initiates transmission of a message from the specified socket to its peer. The send() function sends a message only when the socket is connected (including when the peer of the connectionless socket has been set via connect()). The length of the message to be sent is specified by the len argument.

How many messages per second can socket IO handle?

Both server and client node processes use 95-100% of a CPU core each. So pure throughput looks ok. I can emit 100 messages per second to 100 local clients at 55% CPU usage on the server process.

What is the difference between socket IO and socket IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).


1 Answers

to send a message to a specific client save every one that connects to the server in an Object.

var socketio = require('socket.io'); var clients = {}; var io = socketio.listen(app);  io.sockets.on('connection', function (socket) {   clients[socket.id] = socket; }); 

then you can later do something like this:

var socket = clients[sId]; socket.emit('show', {}); 
like image 164
Philipp Kyeck Avatar answered Oct 02 '22 20:10

Philipp Kyeck