Author Topic: Help me understand Web Sockets  (Read 3358 times)

micah

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
Help me understand Web Sockets
« on: July 31, 2014, 10:08:58 AM »
Last year I started playing around with firebase to replace an app I built that used ajax polling for syncing real-time data.  Apparently web sockets are awesome.

But being ingrained in the world of HTTP, I don't think I've wrapped my head completely around it.  I guess my two questions relate to

1) Bandwidth.  If I have a socket open for 10 minutes straight but just push some JSON data back and forth a few times when it changes, am I using any more/less/same bandwidth then if I polled the server for new information every few seconds?

2) Connection Time.  Can you remain connected for hours on end? Indefinitely? (I mean, other then losing your ISP data connection.)  Could you theoretically write an app that assumes the users are connected 24/7?
"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Help me understand Web Sockets
« Reply #1 on: July 31, 2014, 10:30:28 AM »
So first question, have you ever done normal socket programming?  Not really needed but it helps with understanding websockets.

To your question:

1) When the connection is open there will occasionally be ping/pong traffic.  The packet is tiny (like a few bytes) and doesn't happen that frequently.  Compare that against a full HTTP request and the traffic is going to be much smaller.

2) In theory they can stay open indefinitely.  The purpose of the ping/pong traffic is to keep the channel alive.  In reality it ain't gonna happen.  Device resets, connection resets, minor outages along the route, etc.  Plus, you really don't want to keep a shit ton of ports open for users that aren't active.

micah

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
Re: Help me understand Web Sockets
« Reply #2 on: July 31, 2014, 11:31:08 AM »
No, I've never done any socket programming.  I just read the wikipedia article on the term in general.  The closest I've ever come to caring is when I have to add a port number to an address and even then, I don't really get it.

Thanks for your answers, good to know.

As for an indefinite connection, I guess the practical thing I was curious about was just having something open a very long period.  Like if an app had a dashboard to show real time events and it was being shown on a monitor for most of a day.  Firebase documentation says it handles interrupts/disconnects and automatically reconnects.

So, maybe changing the subject... how do push notifications work?  Like on a mobile device, how does Google Cloud Messaging or Apple push notification service instantly connect to your phone?  Is there an open internet socket open all the time waiting to receive the message?
"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Help me understand Web Sockets
« Reply #3 on: July 31, 2014, 11:49:42 AM »

As for an indefinite connection, I guess the practical thing I was curious about was just having something open a very long period.  Like if an app had a dashboard to show real time events and it was being shown on a monitor for most of a day.  Firebase documentation says it handles interrupts/disconnects and automatically reconnects.
Totally feasible.  As long as you can handle the disconnects gracefully you are golden.

Quote
So, maybe changing the subject... how do push notifications work?  Like on a mobile device, how does Google Cloud Messaging or Apple push notification service instantly connect to your phone?  Is there an open internet socket open all the time waiting to receive the message?
Gonna give the iPhone answer since I know it.  Your phone opens a port to the Apple push notification server and waits.  The external service connects to a different apple server and sends a message that gives a bunch of details like the authorization token (which tells them which device and app) and the details of the notification.  The server processes it and then sends your phone a message which your phone then processes to display the notification.  There is some saving so if your device isn't connected when the notification is sent but connects shortly (not sure on the exact time interval) afterwards you'll get the notification.

micah

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
Re: Help me understand Web Sockets
« Reply #4 on: July 31, 2014, 12:04:01 PM »
good to know.  Thanks!  :thumbsup:
"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."