basically event driven model is that you run into a certain closure (if you don't know what is a closure or how it works, have a look at this thread) when some event happens. So your code:
roomId = socket.on('updateStartTime', function (roomId) {
// this is where the closure starts
return roomId;
// closure ends here
}
It seems like you are trying to get roomId from out side the closure. But this is not gonna work. The mistake in your code is:
the "return roomId" goes nowhere although you are expecting to assign it to the global roomId. Think about it, in a event driven model you never know when will this function(roomId) be executed. Thus when you assign the socket.on result to global roomId, how could it give you the roomId if it's not executed yet? And it's not gonna block there to wait for the result because nodejs' non-blocking model. Actually if you look into the document of socket.on, it returns itself immediately, which is a 'socket'. That's why you get your error.
The 2nd peace of your code works because it's inside the closure. the roomId refers to the local roomId which is an expected value. This code still doesn't give you want though because it's not executed when someone disconnected. Here's something you can try:
socket.on('updateStartTime', function (roomId) {
socket.on('disconnect', function() {
// your 'Room' operation here.
socket.broadcast.emit('updateChat', 'SERVER', socket.username + ' has disconnected...');
});
});
Keep in mind in a event driven model, it's not executed sequentially. Anything can happen at anytime. You need to do the right thing in the right place.