Skip to main content

Real time notifications with Drupal and Go

Senior Developer
Dec 21, 2012

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 also created a Drupal API module for both the service part (creating/deleting notification centers, posting notifications) and the client part (receiving messages from the service with JavaScript).

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.

The project is available at GitHub under GPLv3 license. The Drupal API module is also available on Drupal.org. If you are interested in a hosted version of the service, contact us!

Tamás is responsible for the development and design of custom Drupal modules and back-end solutions. He's a self-taught programmer, with his main focus on Drupal, Go and iOS development. Besides programming languages, he's interested in distributed computing, computer graphics, image processing and security. He has a couple of contributed modules on Drupal.org, and he completed the Summer of Code at Drupal in 2010 and 2011. His bigger projects at Pronovix include participating in Drupal Gardens development, helping to create sites for Warner Music Group, Pfizer and Florida Hospitals. He did performance optimizations on Qatarliving. He's one of the main contributors to the Brightcove module used by large Drupal sites worldwide.

Newsletter

Articles on devportals, DX and API docs, event recaps, webinars, and more. Sign up to be up to date with the latest trends and best practices.

 

Subscribe