0

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?

4
  • Why not use Ratchet and do your websockets server-side stuff in PHP? Commented Jan 13, 2014 at 17:52
  • Because I spent a week trying to make Ratchet work on my server, asking two more people for help in the process, Stack overflow was of no help and, in the end, I gave up because it wouldn't work, there is no documentation to speak of and even if I get it to work I don't want to be stuck with a technology with no real online support in case I run into problems again. It just all seemed very frail and vulnerable. Commented Jan 13, 2014 at 17:54
  • Here's a decent server that works, just in case 4shared.com/rar/7RIos1tuce/PHPWebSocket-Chat-master.html Commented Jan 13, 2014 at 18:05
  • Thanks, I'll look into that. No dependencies? What about support for Opera/Safari? Commented Jan 13, 2014 at 18:10

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

I will look into that, as well as Apache Thrift (mentioned in other answer). It will take a while to research this, and I will post here when I have something to add.
ZeroMQ is a much better fit than RabbitMQ for what you're looking for. RabbitMQ assumes a complex messaging topology, and supports it out of the box, but it's overkill for what you're trying to do. ZeroMQ provides the simple building blocks to build a communication system as simple or complex as you want, it just abstracts away the really low level aspects of building connections.
I'm watching the video now, and it seems to me like I could skip Node altogether if I use ZeroMQ, no? If I understand correctly, I could do the PHP bind for ZeroMQ, then use the Pub/Sub functionality in a "forever iframe" to keep updating all connected clients. Am I missing something?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.