Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io: How to emit an Array and a Variable simultaneously

Learning Node.js, Express.js and Socket.io.

Made a Chat an it works so far.

Now I would like to emit to the Client, that a user has entered or left the chat by emiting a variable that indicates that...

Ist that possible?

So something like this:

Server.js

var users = [];
var inout;
function updateUsers(){
    io.emit('users', users, 'inout', inout);
}

Client:

var socket = io.connect( 'http://'+window.location.hostname+':3000' );
    socket.on('users', function(data){

// how to get the 'inout' here? }

Thanks for any Help... ;)

like image 879
bernd pfeffer Avatar asked Sep 20 '26 14:09

bernd pfeffer


1 Answers

The simplest way is to emit object:

let users = []
let inout

function updateUsers() {
    io.emit('users', {users, inout});
}
like image 132
Paul Rumkin Avatar answered Sep 23 '26 02:09

Paul Rumkin



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!