Using WebSocket with Drupal

Drupal TipsDrupal is written with PHP – a language that has quite a few limitations: request-response logic, one thread per request, no shared-in-memory variables between requests, among other strictures. PHP is easy and fine when you only need to render HTML pages, but what if you want to add real-time applications like chat, streaming analytics, or document collaboration? For these, a better approach is to use the WebSocket protocol. Languages like Java or C++ support this protocol in their core, but not PHP. If we want to develop real-time applications, can a Drupal website avoid writing some part with Java or C++ or using third-party products like Google Cloud Messaging (GCM)? The answer is yes.

One approach is to use NodeJS. Node is built on Google’s V8 engine and operates on a single thread (which can be extended by JXCore for multithreading) and non-blocking IO. It can be used together with the extension real-time app. For the server- and client-side parts, we can use JavaScript code that every developer knows. To see how easy this solution can be, have a look at this explanatory article: Because Node supports MySQL and MongoDB, we can save all data in one database with Drupal main website. Drupal also has an integration module:

For those developers who prefer to use PHP for server-side discussions, will be interesting. ReactPHP and phpDaemon are built with C++ and use the same event loop library as Node: libdev. To see more deeply how ReactPHP works, check out this presentation:

Finally, this comparison between Node and ReactPHP shows that they are almost identical in performance:

Comparison chart of NodeJS, PhpQuery, and reachPHP

1 thought on “Using WebSocket with Drupal

Comments are closed.