socket.io, server emit event before client set up listener for that event
NickName:Srle Ask DateTime:2015-08-09T02:47:06

socket.io, server emit event before client set up listener for that event

In the following code snippet(using node.js and socket.io library), sometimes (and sometimes not) server emit event something before client side set up socket listener for something event which will cause that associated anonymous function will not execute.

Client-side: index.html

<script src="socket.io/socket.io.js"></script>
<script>
   ...
   var socket = io.connect('http://localhost:8080/abc');
   ...
</script>
<script src="/somewhere/test.js"></script>

Content of test.js

...
console.log(new Date().getTime(), 'debugging');

socket.on('something', function(data) {
// will not execute
console.log(data);
});
...

Server-side:

io.of('/abc').on('connection', function(socket) { 
   console.log(new Date().getTime(), 'EMIT SOMETHING');
   io.of('/abc').emit('something', 'b');
});

Result of console.log on the client side: 1439057954676 debugging
Result of console.log on the server side: 1439057954114 EMIT SOMETHING

Why server emit event before client set up listeners in the above example?

Copyright Notice:Content Author:「Srle」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31897363/socket-io-server-emit-event-before-client-set-up-listener-for-that-event

More about “socket.io, server emit event before client set up listener for that event” related questions

socket.io, server emit event before client set up listener for that event

In the following code snippet(using node.js and socket.io library), sometimes (and sometimes not) server emit event something before client side set up socket listener for something event which will

Show Detail

How to emit an event in response to an event using socket.io?

I am new to socket.io, and trying to get it to work. I don't know what I am doing wrong here, as it seems super straightforward - yet it is not working. I suppose I am missing something obvious.

Show Detail

Is it possible to limit socket.io to have only one listener per event?

my question is if its possible to limit the bind listener possibility for specific event? For example I have a listener: //In server socket.emit('something', { message: 'Hello World!' }); //At cl...

Show Detail

Node.js socket.io - client emit not seen on server

I'm trying to put together a simple Socket.IO test case in Node.js, but I'm not seeing the events emitted from the client getting to the server. var http = require("http"), io = require("socke...

Show Detail

Socket.io event won't fire and cannot emit from client

So i was trying to set a new project with nodejs and socket.io at first i try these codes in my home pc at this site: http://socket.io/#how-to-use i tried the first example and it works well so i

Show Detail

Socket.io: emit no event

I try to emit event "player move" with a json-object: this.broadcast.emit("player move", JSON.stringify(player)); but on the client I get this: INFO: &gt; 5:::{"args":[{"x":108,&qu

Show Detail

Socket.io event listener not working on client side

this is my index.html &lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Socket.IO chat&lt;/title&gt; &lt;script src="/socket.io/socket.io.js"&gt;&lt;

Show Detail

NodeJS/Socket.io not receiving event from client

While trying to setup a simple NodeJS server and Socket.io client to test something with WebSockets, I've stumbled into something stupid. I'm pretty sure it's something stupid I've done because I've

Show Detail

socket, emit event to server from server

I have a server set up and client -> server events work great, server -> client events also work. However, as I am writing to my DB I want to emit a server->server (or server->self) event so I can

Show Detail

emit a local event with socket.io

I'm using socket.io on the server side (and client). What I want to do is trigger an event on disconnect. But it doesn't seem to get picked up locally. In the node server I have: sock.on('chat:l...

Show Detail