Real time notifications with Drupal and Go
Because of its limitations, PHP and therefore Drupal can't send notifications to the browser real time. Pinging a URL will be slow (because Drupal has to bootstrap every time), and keeping HTTP connections alive (long polling or COMET) will definitely kill Apache. You have to face this restriction every time a project requires the use of real time notifications, so we’ve come up with a solution.
After reviewing our options for existing solutions, we decided to write our own simple service, which can do one thing only, but do it very well. After considering different technologies, we came to the decision to use Go. It's very easy to deploy (one statically linked ELF executable and a few template files), and it has a very small footprint (it can sit on the production server without noticeably slowing it down). The language itself is very clean and readable. The built-in tools and integrations to different editors make it easy to develop with, despite the language being just a few years old. Its way of handling concurrency (goroutines) makes programming really easy, as handling thousands of connections is not a problem. What I can tell from my experience is that the most surprising thing about Go is how productive I am with it. I was able to solve complicated tasks very fast after using it for a few days.
The architecture of the service is really simple: the client connects to the service with websockets or just pings a given URL (for browsers without websockets support). When something happens, e.g. a comment is posted, Drupal sends a request to the service, and the service starts delivering it to the connected clients. The message doesn’t have a specified format, it's just plain text. If you want to use data structures, you can use any serialization format (JSON, XML, etc). The server has multiple notification "hubs", called notification centers, and it can handle multiple posting users, so more than one project can use the same server.
We are already using this technology with Scoreshare, this is how you get immediate update when the score changes. You can read more on this soon in our technical background post for Scoreshare.