Pronovix articles

Real time notifications with Drupal and Go

by Tamas Demeter—Haludka / Friday, December 21, 2012 - 15:12

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!

Comments

Thanks for sharing your experience and code ;)

I believe node.js would have made that possible too. I'm interested to hear if you have considered it and what was your findings.

Thanks.

Node.js would have made it possible, but if I'm not mistaken things have to be run through node afterwards? With go deployment is as simple as doing a proxy pass to the port the go program is listening to.

Go also has static typing, so you don't have to worry about runtime errors coming back to bite you as much, since most of them you find upon compiling.
Go also doesn't compile a program if you try to import dependencies you aren't using, or anything for that matter.

Another thing that comes to mind is how useful the contract an interface creates between the current api and future code.

On a personal note, I find javascript doesn't feel like a well designed language.

Feel free to correct me anywhere I'm wrong, just sharing my opinion with everything I currently believe to be correct ;)

Thanks for sharing your experience and code! I give 10 out of 10 for this research!

Add new comment

By submitting this form, you accept the Mollom privacy policy.