Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass data from the client-side to my nodejs server using socketio?

Using socketio on the client side and nodejs with socketio on the server side, how would I go about passing data from the client side to the server? Would I use an emit function with socketio on the client side?

like image 332
John Avatar asked Dec 18 '25 10:12

John


1 Answers

Yes. Client side or server side you simply emit events and handle events.

client side:

var socket = io.connect('http://localhost');
socket.emit('my other event', { my: 'data' });

server side:

io.sockets.on('connection', function (socket) {
  socket.on('my other event', function (data) {
    console.log(data);
  });
});
like image 73
generalhenry Avatar answered Dec 19 '25 23:12

generalhenry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!