sveti petar
sveti petar

Reputation: 3797

Sending structured data from PHP to Node.js without HTTP?

I have a PHP application in the CodeIgniter framework, running on an Apache server with a MySQL database. There's a lot of database communication, all done through an abstraction layer. It's a game application and, when the user opens the page, a list of open games with some additional info is fetched from MySQL using PHP and shown on the page. Now, I need to set it up so the list updates live. I need a websockets server for this because the lag is too big when I use Ajax, and I've decided to go with Node.js + sockets.io for the implementation. I don't really need to contact the database every time, I can update the list based on user actions on the client.

This is where I'm stuck - after loading the list with PHP the first time, how do I send it over to Node.js? I found a solution that involves sending a HTTP request for this purpose, but this seems redundant since, at this point, I already have all the data displayed on the client.

Here's some sample data, in a PHP array before being displayed on the page:

array(2) { [0]=> object(stdClass)#19 (6) { ["id"]=> string(1) "1" ["admin_id"]=> string(1) "2" ["start_date"]=> string(19) "2013-11-18 07:19:46" ["status"]=> string(1) "1" ["username"]=> string(15) "Marko Marković" ["option"]=> string(5) "start" } [1]=> object(stdClass)#20 (6) { ["id"]=> string(1) "2" ["admin_id"]=> string(1) "3" ["start_date"]=> string(19) "2013-11-18 07:20:32" ["status"]=> string(1) "1" ["username"]=> string(13) "Dzoni Noksvil" ["option"]=> string(5) "start" } } 

Is there a way I can package this up on the client and then just have Node pick the data up from the client, so there would be no need to send the data from PHP directly to Node?

Upvotes: 0

Views: 1010

Answers (2)

Steve
Steve

Reputation: 1423

If all your domain logic exists in PHP and you simply want to send the final set of data to Node (or any language) I would consider doing it like this: Use a Message Queue or a Message Broker.

You are trying to do this:

PHP
  |
Node.js

Consider this:

PHP
 |
Message Queue
 |
Node.js

Once you have a common interface between the two languages communication becomes easy. PHP can push completed stuff into your queue and node.js can pick it up for use. If you decide to go this route please update your post with your findings, would love to hear how it went.

  • RabbitMQ
  • ZeroMQ (kind of more like a socket system library) - reads lots of good things
  • Great video explaining ZeroMQ in detail

Upvotes: 2

John D.
John D.

Reputation: 2599

You could use a library for inter-process communication, such as Apache Thrift. There are bindings for Node and PHP. But, if this is all running on one server and you want to keep things really simple, you could have PHP write to a file (perhaps serialized as JSON) and have Node consume that file.

Upvotes: 0

Related Questions