Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook live feed functionalty?

I am trying to make something like facebook live feeds, for example: when someone likes something or comments on something, the page updates without refreshing it! I want to know which is the proper way to do this? regards

like image 587
Sabri Aziri Avatar asked Feb 03 '12 23:02

Sabri Aziri


1 Answers

Realtime updates in a web application is a hard problem because a single server handling many simultaneous long-lived TCP connections is a hard problem.

This is essentially impossible on a traditional web server like Apache + PHP because it allocates an entire OS thread for each incoming connection. Threads have significant overhead (like ~2 MB of RAM just for the stack space, plus whatever heap memory your application needs), so as few as a few hundred clients having your page open at the same time can bring a small server to its knees, and even an extra-large (and extra-expensive) hundred-GB-of-RAM server can only handle a few thousand concurrent connections.

Realtime communications is where Node really shines. Its single-threaded, event-driven architecture can easily support 2,000 concurrent connections on a commodity laptop, because each incoming connection is a small (a few kilobytes) heap allocation. The limiting factor actually becomes the CPU and the underlying OS's TCP stack.

My recommendation is to take a look at Node – this is exactly the kind of problem it is designed for. You already know JavaScript, so it's really just a matter of the API and mastering Node's async, event-driven nature.

You'll probably want to use Express for your HTTP server needs and use Socket.io for the realtime communications.

Socket.io is especially wonderful because its client-side library abstracts away all of the drudgery of cross-browser support:

  • In A-grade browsers, it connects to your server via WebSockets. This gets you a TCP socket that remains connected indefinitely, over which you can push arbitrary data at any time.
  • In downlevel browsers, it uses a fallback mechanism:
    • A Flash-based transport like WebSockets, but requires Flash player (if available)
    • AJAX long polling
    • And some more esoteric fallbacks if neither of those work
like image 154
josh3736 Avatar answered Sep 23 '22 04:09

josh3736