2

Say A sends a message to B and waits for a callback and then A probably sends new messages and B also sends many messages to A.

What I mean other message exchanges happen during this time before callback happens.

Does it create a race condition or block other message sending until the first callback is complete or does it enforce the order of callbacks so that say the callbacks of message 1,2,3,4,5 always arrive in the same order as the message was sent out?

Assistance would be much appreciated.

Konul
  • 51
  • 5
  • The callbacks happen locally. The callback function is not passed to the server. – mscdex Jan 30 '15 at 01:59
  • 1
    But say A sends message to B, the callback to A is an acknowledgement that B received A's message. How can it know locally that the message was delivered? My understanding is A sends, server gets it and passes to B, then B confirms to server, and then server sends confirmation to A. Not sure I am right, but I would guess that is what the callback means. (?). – Konul Jan 30 '15 at 02:07
  • Yes, you are right about how it works. – mscdex Jan 30 '15 at 02:11

1 Answers1

2

Well, the question involves a number of concepts - so hard to answer fully. I will try to t least give partial response or insight. If you had provided details, why it matters for your purpose - it could help to better target the answer.

One of the advantages of nodejs is that it is a single-threaded, event-driven, non-blocking I/O model - which means there is almost no or minimum blocking (at least theoretically). See conceptual model here

However, some trivial blocking should happen due to transport, consistency etc [?]. But it shouldn't be a problem as this will extremely insignificant and happens in all programs no matter what language it uses. Secondly about sockets. The concept of socket considers thatit can be blocking or non-blocking depending on your purpose. Blocking and Non-blocking sockets Blocking doesnt necessarily mean it is bad.

Thirdly, even if there is no blocking, still events dont really happen in parallel. I mean even if A and B send messages to each other very frequently - there is a time gap between them - although trivial for humans. That difference can even be expressed even in millionth of second. Can you really send over million messages in a second? So, even if callback has some impact - you should ignore it for the purpose of your program. Also, even if they occur at the same time, javascript can do one thing at a time - so at the end when you receive, you should do them one at a time. For example, if you want to display or alert a message, they will be one at a time.

As to ordering of the messages, Node.js is a single event loop. So, my understanding is it runs a non-stop loop and waits for events, and emits information in the order the events occur. For example Understanding nodejs event loop

 while(new Date().getTime() < now + 1000) { // do nothing }

So, for your purpose, I would say unless B sends a message between A sending a message and server receiving it, you should receive a callback before anything else. Simply ordering happens in the order the nodejs server receives it. Hope it helps.

Selay
  • 6,024
  • 2
  • 27
  • 23
  • 1
    Thanks for detailed reply. The reason why care about race condition and ordering is that if many messages stack up, the browser may crash? But what I see from your explanation is probably I shouldn't care about it that much. Thank you. – Konul Jan 30 '15 at 02:29
  • 1
    Yes, don't pay much attention to details. Socket connection lasts long and is cheaper than normal communication. So, wouldn't cause browser crashes as you think. – Selay Jan 30 '15 at 02:32