2

I've been reading a few posts on here regarding polling and even had a look at Pusher although i don't want to go down that route and need some advice in regards of making an efficent notification system. How do facebook, twitter and other websites do this? are they using web sockets?

unleashed
  • 29
  • 1
  • 3
  • AJAX long-polling is technically most efficient without a WebSockets push, though technically it depends on what event you are trying to be notified of – jerluc Jun 28 '11 at 19:30

2 Answers2

15

Polling

> Polling data from server - most
> efficient and practical way/setup

You should avoid polling because it is not efficient and also not real-time at all. Let's say you poll every 30 seconds and your server can handle the load without any problems. The problem then is that your data is not real-time. You could shorten the poll-interval (every second), but then you will have a very difficult time trying to scale your server.

But sometimes polling (smart) is also very nice, because it is easy to implement. Some tips I have for you are:

  • Don't use the database, but retrieve data from in-memory database like redis, memcached because they are much faster. That is the secret ingredient for most popular big players(websites) to run smoothly. Facebook has special purpose servers that use a lot of memory using memcached (5 TB in 2008 => Facebook has grown a lot since ;)).

    If you can't install (probably should!) Memcached or Redis on your server you could consider using the hosted http://redistogo.com which is free for small sites.

  • The other thing to do is increment the poll interval using github's library to prevent server overloading.


> How do facebook, twitter and other
> websites do this? are they using web sockets?

Web-sockets

Some of these sites are using websockets, but that is only one of the many transports that they support because websockets aren't available in all browsers. In the future when all browsers support websockets that will be the only transport used (probably). Below I will give you a list of all the popular transports with a quick description:

WebSocket is a technology providing for bi-directional, full-duplex communications channels, over a single Transmission Control Protocol (TCP) socket. It is designed to be implemented in web browsers and web servers, but it can be used by any client or server application.

For the client side, WebSocket was to be implemented in Firefox 4, Google Chrome 4, Opera 11, and Safari 5, as well as the mobile version of Safari in iOS 4.2. However, although present, support is now disabled by default in Firefox and Opera because of concerns over security vulnerabilities.

For the most part, XMLHttpRequest long polling works like any standard use of XHR. The browser makes an asynchronous request of the server, which may wait for data to be available before responding.

This transport is available in every browser.

A long-polling Comet transport can be created by dynamically creating script elements, and setting their source to the location of the Comet server, which then sends back JavaScript (or JSONP) with some event as its payload. Each time the script request is completed, the browser opens a new one, just as in the XHR long polling case. This method has the advantage of being cross-browser while still allowing cross-domain implementations.

provide a usable streaming transport in Internet Explorer

XMLSocket is a class in ActionScript which allows Adobe Flash content to use socket communication, via TCP stream sockets. It can be used for plain text, although, as the name implies, it was made for XML. It is often used in chat applications and multiplayer games.

Facebook

As you probably know Facebook does use PHP for there active development, but actually don't use it for any of there real-time elements on there site, because PHP is not designed to handle this properly (yet?). A lot of people get mad at me for saying this, but I can't help that it is the truth (even Facebook agrees). In PHP almost all function calls (C) are using blocking I/O which makes scaling real-time systems almost impossible. I read a blog post over here using non-blocking IO with PHP (quality?). In the past Facebook created the chat using Erlang which is also pretty popular for doing non-blocking IO. I myself find the Erlang code looking strange, but I still would like to learn it. Here are some links about Facebook using Erlang:

Also Facebook bought Friendfeed in the past and open-sourced there Tornado framework which is written in Python to do non-blocking IO.

It is no longer just the traditional Linux, Apache, MySQL, and PHP stack that make a site like Facebook or FriendFeed possible, but new infrastructure tools like Tornado, Cassandra, Hive (built on top of Hadoop), memcache, Scribe, Thrift, and others are essential. We believe in releasing generically useful infrastructure components as open source (see Facebook Open Source) as a way to increase innovation across the Web.

I assume they are also using tornado for some parts of there system now.


Other sites

Below I will try to list some popular frameworks(open-source) to do non-blocking IO:

  • Socket.io (Node.js): Socket.io is becoming a pretty popular non-blocking framework for node.js (1722 people are watching this project on Github right now). I really like Socket.io
  • Netty (Java): The Netty project is an effort to provide an asynchronous event-driven network application framework and tools for rapid development of maintainable high performance & high scalability protocol servers & clients.
  • tornado (Python): Tornado is an open source version of the scalable, non-blocking web server and tools that power FriendFeed.

> even had a look at Pusher although I
> don't want to go down that route

I don't understand your dislike for pusher because it is a pretty popular hosted solution. With this hosted solution you could started building scalable real-time service without any hassle with pretty good pricing (free if small and pretty affordable for middle range websites).

Alfred
  • 60,935
  • 33
  • 147
  • 186
1

I was researching about websockets and came to know about 'Kaazing WebSocket Gateway'. Kaazing WebSocket Gateway provides complete WebSocket emulation for all the older browsers (I.E. 5.5+, Firefox 1.5+, Safari 3.0+, and Opera 9.5+), so you can start using the HTML5 WebSocket APIs today.

Please see this link

Nick
  • 1,799
  • 3
  • 23
  • 32