I have a clientid and username and i want them both send with the socket.
client.userid = userid;
client.username = username;
client.emit('onconnected', { id: client.userid, name: client.username });
i tried this for example but it doesn't seem to work
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.
Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).
info. WebSocket is a communication protocol which provides a full-duplex and low-latency channel between the server and the browser.
emit('clientEvent', 'Sent an event from the client!' ); </script> <body>Hello world</body> </html> To handle these events, use the on function on the socket object on your server. var app = require('express')(); var http = require('http').
You can try this
io.sockets.on('connection', function (socket) {
socket.on('event_name', function(data) {
// you can try one of these three options
// this is used to send to all connecting sockets
io.sockets.emit('eventToClient', { id: userid, name: username });
// this is used to send to all connecting sockets except the sending one
socket.broadcast.emit('eventToClient',{ id: userid, name: username });
// this is used to the sending one
socket.emit('eventToClient',{ id: userid, name: username });
}
}
and on the client
socket.on('eventToClient',function(data) {
// do something with data
var id = data.id
var name = data.name // here, it should be data.name instead of data.username
});
Try sending the object as a whole:
$('form').submit(function(){
var loginDetails={
userid : userid,
username : username
};
client.emit('sentMsg',loginDetails);
}
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