Photo: The National Archives (UK)

Hybrid PHP and Node.js web applications

Michał Męciński
5 min readMay 11, 2017

Full-stack JavaScript applications are the future of the web. However, hybrid web applications, which combine the traditional LAMP stack with the capabilities of Node.js, are often a reasonable alternative.

There are many situations when a hybrid approach can be considered:

  • You have an existing application written in PHP. Porting everything to JavaScript would require a lot of work. By adding new features based on Node.js to an existing PHP application, you can often achieve a similar effect with much smaller effort.
  • Your team consists mostly of skilled PHP programmers. It’s never a bad idea to start learning a new technology, but having the entire team switch to a new platform is quite risky, especially if you are on a tight schedule.
  • The application combines interactive or real-time features with static or traditional web content. You can leverage the strengths of both Node.js and PHP.

Note that I’m using PHP as an example, but you can substitute it with Perl, Python or whatever language you prefer.

Best of both worlds

There are common approaches to creating web applications:

  • In the classic model, a web application consists of many separate pages. Links are used for navigation and forms are used to perform actions. Such applications are easy to implement using Apache, MySQL and PHP, the technologies often referred to as the LAMP stack. There is a lot of tools and frameworks that you can use, and most web developers are familiar with these technologies.
  • In the single-page application model, all content is loaded, updated and modified dynamically and there is no need to reload the entire page. Such applications rely heavily on AJAX or WebSockets. They are typically easier to implement using a Node.js server and a modern client-side framework such as React or AngularJS.

These two models are almost always presented as mutually exclusive. But in reality, most modern web applications combine both models. For example, the timelines in Facebook or Twitter are single-page applications — they are fully interactive and updated in real time. However, if you look closely, these applications also have a whole bunch of additional pages for changing settings and preferences, displaying help articles, etc. These pages consist mostly of static content and simple forms.

So why not combine the best of both worlds? It is possible to use a hybrid model, where the interactive part is written using JavaScript and Node.js, and the additional pages are generated using a traditional LAMP stack.

Real life examples

Currently at Bulletcode we are working on two applications which use such hybrid model:

  • Frienzzle is a multi-player browser game. The “heart” of the game is a page which contains a large HTML canvas, where players can play jigsaw puzzles with other people in real time. The game uses a Node.js service to coordinate actions performed by players. However, there are also many pages which are generated on the server using PHP, such as the list of recent games, pending invitations, the gallery of pictures, etc.
  • A search engine for bus connections between selected cities. The main page consists of a search bar, where users can change search criteria, and a dynamically reloaded list of results. The results are fetched from three different APIs and combined together using a Node.js service. The application also has an administration panel which is implemented as a traditional web application in PHP.

This hybrid approach works great in both cases and we will definitely use it in our future projects.

Technical solution

The hybrid model is not much more complicated than a regular LAMP stack. We use a typical Linux host with Apache, PHP and MySQL. We also use forever-service to install Node.js applications as Linux services on the same host. Apache handles HTTP and HTTPS requests on ports 80 and 443. The Node.js services use various different ports to handle incoming AJAX requests and WebSocket communication. That’s the simplest approach; you can use separate hosts for Apache and the Node.js services for better scalability.

Both PHP and Node.js can access the same MySQL database, so the entire communication between PHP and Node.js can be handled at the database level. If that’s not enough, you can always use a cache server such as Memcached or Redis.

This is only a high level overview. I will continue writing about this subject in the following weeks, focusing on specific technical problems that we encountered and the solutions that we used.

Final notes

The hybrid model has many benefits and it’s often much better than sticking to one particular technology. However you have to consider the additional costs of such approach:

  • Developing and maintaining your application requires having both PHP and JavaScript developers in your team.
  • Some code needs to be duplicated in both technologies. For example, both PHP and Node.js will need a mechanism for authorization, logging, etc.

While the hybrid model is not a silver bullet, it’s definitely worth considering when designing a new web application or extending an existing one.

Update: I wrote an article which provides some more practical tips for implementing and installing a Node.js service on a web server.

Last week I announced that Frienzzle will be released by the end of May. It’s been a busy week at Bulletcode, but I finally managed to close the most outstanding tasks and today I started working on Frienzzle again.

Next week I will write about an interesting design problem that I’m currently trying to solve. In the meantime you can take a peek at frienzzle.com and subscribe so you don’t miss the release.

--

--