Be a Supporter!

Receiving asynchronous Web data

  • 333 Views
  • 8 Replies
New Topic Respond to this Topic
Momo-the-Monkey
Momo-the-Monkey
  • Member since: Oct. 15, 2005
  • Offline.
Forum Stats
Member
Level 45
Musician
Receiving asynchronous Web data 2013-02-05 18:13:21 Reply

I might be missing something but asynchronous data transfer has always confused me a bit. For instance, Facebook's chat or an iPhone or Android app like GroupMe. How do they constantly update? From what I can think of, an infinite loop while constantly checking and rechecking the server is needed. Such as a while loop or a timeout function.
Can anyone help me understand this concept a little better?


Hello, from the past! [ PHP: Main ]

BBS Signature
citricsquid
citricsquid
  • Member since: Jun. 25, 2005
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to Receiving asynchronous Web data 2013-02-05 18:52:14 Reply

an infinite loop while constantly checking and rechecking the server is needed

Pretty much, it's very very easy. The "easiest" way that will work in most browsers is long-polling, open a request and keep it open until there is data then open another, rinse and repeat. There's also websockets, but I believe the support for that is limited to modern browsers, some systems will fall back on Flash when using websockets.

Super simple PHP example that provides some extra information: http://stackoverflow.com/a/333884/200518

Web sockets:

http://www.websocket.org/
http://www.html5rocks.com/en/tutorials/websockets/basics/
http://net.tutsplus.com/tutorials/javascript-ajax/start-usin g-html5-websockets-today/

There's also a wide variety of services that will provide you with a simple API and do all the work for you:

http://www.pubnub.com/ (I use PubNub, they're pretty great, very good pricing and super simple)
http://pusher.com/

And there's open source alternatives that you can run yourself:

http://socket.io/
http://faye.jcoglan.com/

misc:

http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_patte rn
http://en.wikipedia.org/wiki/Comet_(programming)

Momo-the-Monkey
Momo-the-Monkey
  • Member since: Oct. 15, 2005
  • Offline.
Forum Stats
Member
Level 45
Musician
Response to Receiving asynchronous Web data 2013-02-05 20:03:06 Reply

Brilliant! Thank you, that's exactly what I was looking for.


Hello, from the past! [ PHP: Main ]

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Receiving asynchronous Web data 2013-02-05 21:00:34 Reply

At 2/5/13 06:13 PM, Momo-the-Monkey wrote: From what I can think of, an infinite loop while constantly checking and rechecking the server is needed.

That's pretty much how any application that receives input works.
The Windows and X11 APIs both work that way, and any server that implements HTTP does as well (as three examples; there are many more).

Basically if you want to receive some sort of input over an indefinite period of time then you will need an infinite loop that continuously checks for new input.

Momo-the-Monkey
Momo-the-Monkey
  • Member since: Oct. 15, 2005
  • Offline.
Forum Stats
Member
Level 45
Musician
Response to Receiving asynchronous Web data 2013-02-08 22:44:28 Reply

At 2/5/13 09:00 PM, Diki wrote: Basically if you want to receive some sort of input over an indefinite period of time then you will need an infinite loop that continuously checks for new input.

Okay, i'm a little confused about the server's response though. Does it just sleep until new data arrives? Or does it sleep for a period of time and then send any and all new data? I'm just not sure how to send data from the server to the page without losing information or without sending all the information all the time.


Hello, from the past! [ PHP: Main ]

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Receiving asynchronous Web data 2013-02-08 23:45:32 Reply

At 2/8/13 10:44 PM, Momo-the-Monkey wrote: Does it just sleep until new data arrives?

Depends if the server is implemented as blocking or non-blocking.
A blocking operating will cause the server to sleep until a situation occurs that it determines to end said blocking, such as receiving data via a socket.
A non-blocking operating will just continually perform checks, and possibly raising an exception when there's nothing to do (that's how Python does it for example).

At 2/8/13 10:44 PM, Momo-the-Monkey wrote: I'm just not sure how to send data from the server to the page without losing information or without sending all the information all the time.

Do you mean sending HTML for a webpage?
There's nothing that would cause that situation to happen. Just send the data through your socket and the browser will receive it; the browser will only receive data as many times as you send it. Easy peasy.

Also, since web servers use TCP there's no chance of data being lost (provided the connection isn't lost). TCP guarantees that any data sent will be received.

Momo-the-Monkey
Momo-the-Monkey
  • Member since: Oct. 15, 2005
  • Offline.
Forum Stats
Member
Level 45
Musician
Response to Receiving asynchronous Web data 2013-02-09 09:18:11 Reply

At 2/8/13 11:45 PM, Diki wrote: Do you mean sending HTML for a webpage?

Good point. Okay, what about non-web servers? say a video game or an application? Of course this is a gross generalization. I image each system or language would have a different implementation.


Hello, from the past! [ PHP: Main ]

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Receiving asynchronous Web data 2013-02-09 13:00:44 Reply

At 2/9/13 09:18 AM, Momo-the-Monkey wrote: Okay, what about non-web servers? say a video game or an application?

Video games should use UDP which does not guarantee the data will be sent, and also does not guarantee they will be sent in the correct order.

At first it might seem a little odd that you would not want a guarantee of your packets being delivered, but with video games it does make sense. When you're playing an online game you only care about the most recent packets. Say, for example, you're playing a shooter (such as Team Fortress 2); you'll want the position of your enemies to always be up to date otherwise you may end up shooting at where they used to be, and not where they actually are. That could end up happening in TCP since if a packet gets lost it will halt transmission of newer packets until the lost one is successfully sent.

Another good example of when to use UDP is with a VoIP application (such as Skype), or a live webcast stream. Basically if you need something done in real time you need UDP, otherwise you should probably use TCP.

You can read more about UDP vs TCP here.

At 2/9/13 09:18 AM, Momo-the-Monkey wrote: I image each system or language would have a different implementation.

For the most part the implementations of TCP and UDP will be the same because they have to follow their specifications. A few small details might be different but the important things will be the same.

Momo-the-Monkey
Momo-the-Monkey
  • Member since: Oct. 15, 2005
  • Offline.
Forum Stats
Member
Level 45
Musician
Response to Receiving asynchronous Web data 2013-02-10 00:22:15 Reply

At 2/9/13 01:00 PM, Diki wrote: You can read more about UDP vs TCP here.

Wow thanks! That clears things up a bit.


Hello, from the past! [ PHP: Main ]

BBS Signature