emit immediately after io.connect?

764 views
Skip to first unread message

Felix E. Klee

unread,
Oct 9, 2012, 5:15:53 AM10/9/12
to sock...@googlegroups.com
Client code, to send an event to the server:

socket = io.connect(host);
socket.emit('my event', {my: 'data'});

Will the event be queued until the connection is established, or will it
be lost?

IOW: Am I required to write code as below?

socket = io.connect(host);
socket.on('connect', function () {
socket.emit('my event', {my: 'data'});
});

Veerabhadraiah A S

unread,
Oct 10, 2012, 8:47:08 AM10/10/12
to sock...@googlegroups.com
Yes, you are on the right track..:)
--
Thanks and Regards,
Veerabhadraiah A S,
Software Engineer,
Above Solutions India Pvt Ltd,
Indira Nagar,
Bangalore.



Felix E. Klee

unread,
Oct 10, 2012, 10:00:20 AM10/10/12
to sock...@googlegroups.com
On Wed, Oct 10, 2012 at 2:47 PM, Veerabhadraiah A S
<veerabha...@gmail.com> wrote:
> Yes

In reply to which of my questions?

cayasso

unread,
Oct 14, 2012, 5:04:47 PM10/14/12
to sock...@googlegroups.com
Why wont you try it and see what happen?

JB

Marc Bachmann

unread,
Oct 14, 2012, 5:06:12 PM10/14/12
to sock...@googlegroups.com
Hi,

my events are queued client side. I haven't tested whether the messages are queued server side. 
And I don't know how long the queue can be.

Yea just try disconnecting the internet.

Felix E. Klee

unread,
Oct 22, 2012, 4:48:15 PM10/22/12
to sock...@googlegroups.com
As suggested by some of you, I did tests (using the developer console in
Google Chrome):

* (A) This works (i.e. server receives 'event'):

1. server up

2. `socket = io.connect(host); socket.emit('event');`

* (B) This works:

1. server up

2. `socket = io.connect(host);`

3. server down

4. `socket.emit('event');`

5. server up

* (C) This doesn't work:

1. server down

2. `socket = io.connect(host); socket.emit('event');`

3. server up

So, events do get queued, if:

* (A) a new connection is about to be created, or

* (B) a connection exists and the server is temporarily down.

Finally, concerning my question:
> Am I required to write code as below?
>
> socket = io.connect(host);
> socket.on('connect', function () {
> socket.emit('my event', {my: 'data'});
> });

Answer: No, I am not required to write code like that. Instead I can
write:

Gustavo Machado

unread,
Nov 2, 2012, 10:54:26 AM11/2/12
to sock...@googlegroups.com
I believe those two examples have different behaviours.

>     socket.on('connect', function () {
>         socket.emit('my event', {my: 'data'});
>     });

Will send 'my event' every time you connect/reconnect.

>socket = io.connect(host);
>socket.emit('my event', {my: 'data'});

Will send 'my event' only once no matter what.

Having said that, if you are sending "identity" information, you might be better off the first approach.

Regards,
Gustavo

Felix E. Klee

unread,
Nov 3, 2012, 4:22:59 PM11/3/12
to sock...@googlegroups.com
On Fri, Nov 2, 2012 at 3:54 PM, Gustavo Machado <mach...@gmail.com>
wrote:
>> socket.on('connect', function () {
>> socket.emit('my event', {my: 'data'});
>> });
>
> Will send 'my event' every time you connect/reconnect.

Thanks, I was supposing that `connect` only fires on initial connect,
not on reconnect.

>>socket = io.connect(host);
>>socket.emit('my event', {my: 'data'});
>
> Will send 'my event' only once no matter what.

That's what I need.

Your message appears just in time. I hope that tomorrow I'll finally
have a free day to continue development. :-)
Reply all
Reply to author
Forward
0 new messages