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! :(
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.
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.
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.
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).
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', {});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With