Best way to send data between php and Node

2,963 views
Skip to first unread message

steven loe

unread,
Apr 30, 2012, 1:06:50 PM4/30/12
to nod...@googlegroups.com
What's the best technique for sending small amounts of data back and forth between node and php on the same server? Is dNode the best solution for this? Or should I be looking at alternatives else?

Thank you!

mscdex

unread,
Apr 30, 2012, 1:30:33 PM4/30/12
to nodejs
Well, it depends on your needs really, but IMHO a persistent TCP
connection would probably be the simplest and easiest.

mscdex

unread,
Apr 30, 2012, 1:31:33 PM4/30/12
to nodejs
On Apr 30, 1:06 pm, steven loe <steven....@gmail.com> wrote:
I'm also curious as to the reason for this bridging in the first place?

Chris Rhoden

unread,
Apr 30, 2012, 1:36:47 PM4/30/12
to nod...@googlegroups.com
Yeah, my feeling is that if you're asking this question, you probably don't have the problem modeled right. In most cases, a shared datastore or a message queue (like 0mq) will do just fine.


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en



--
chrisrhoden

mscdex

unread,
Apr 30, 2012, 2:29:56 PM4/30/12
to nodejs
On Apr 30, 1:36 pm, Chris Rhoden <carho...@gmail.com> wrote:
> Yeah, my feeling is that if you're asking this question, you probably don't
> have the problem modeled right. In most cases, a shared datastore or a
> message queue (like 0mq) will do just fine.

What I was getting at was why not do both tasks in node and take PHP
out of the picture?

Chris Rhoden

unread,
Apr 30, 2012, 2:31:45 PM4/30/12
to nod...@googlegroups.com
Never had to work on a legacy system, or deal with a task which is inherently synchronous? That's 2 reasons right there.

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en



--
chrisrhoden

Mark Hahn

unread,
Apr 30, 2012, 2:43:05 PM4/30/12
to nod...@googlegroups.com
 or deal with a task which is inherently synchronous?  

Why does PHP have an advantage there?  It is easy to make tasks synchronous in node.  You just don't start a second until first is finished.  I have to do that all the time.

Chris Rhoden

unread,
Apr 30, 2012, 2:46:40 PM4/30/12
to nod...@googlegroups.com
On Mon, Apr 30, 2012 at 2:43 PM, Mark Hahn <ma...@hahnca.com> wrote:
 or deal with a task which is inherently synchronous?  

Why does PHP have an advantage there?

Because most of the libraries available for PHP are blocking and you never need to know what a nested callback is.

Look, I am not defending PHP here. I stopped writing PHP years and years ago, but can we stop pretending that node is good for everything? It's the best thing I have found for building fast servers, but there's a whole range of tasks for which it is brutally painful.
 
It is easy to make tasks synchronous in node.  You just don't start a second until first is finished.  I have to do that all the time.

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en



--
chrisrhoden

Mark Hahn

unread,
Apr 30, 2012, 2:56:12 PM4/30/12
to nod...@googlegroups.com
Look, I am not defending PHP here 

And I'm not deriding it.  

I'm just arguing against the point that PHP has some advantage with sequential tasks.  The use of asynchronous callbacks is unrelated to the sequencing of tasks. They are two different levels of the process.  Most of my tasks are synchronous and all of my callbacks are async.  Having tasks run in parallel is quite unusual.

Chris Rhoden

unread,
Apr 30, 2012, 3:05:55 PM4/30/12
to nod...@googlegroups.com
You're completely missing (ignoring?) what I was getting at. I am not talking about whether or not the tasks need to happen exclusively, I am talking about the task itself being, for instance, pipeline oriented.

You are always welcome to write code however you feel like, but I am much happier writing blocking code when I have a sequence of things to do to something. 

obj.method1(function (result1) {
  result1.method2(function (result2) {
    result2.method3(function (result3) {
       ...
       resultn.methodn(function (resultnplusone) {
          // done!
       }
    }
}

Doesn't read super well for me.

newobj = obj.method1().method2();
newobj.method3("string")

Does read well for me. 

If your "task" code looks like this in node, it means you're not serving requests while the task is executing, which means you will need to spin off another process. Which means you will need to communicate with another process. Which means use whatever you're most comfortable with.


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en



--
chrisrhoden

Mark Hahn

unread,
Apr 30, 2012, 3:43:05 PM4/30/12
to nod...@googlegroups.com
Doesn't read super well for me. 

task1 = doTask1 args, task2
task2 = doTask2 args, done

Reads just fine for me.  This is what most of my code looks like.

steven loe

unread,
Apr 30, 2012, 3:56:18 PM4/30/12
to nod...@googlegroups.com
Hi Folks,

My intent is not to start a religious debate. Truly. I'm working with a legacy system in php. Rewriting it from the ground up is not an option at the moment. I have a flash client that currently gets commands from php by polling the php server every 10 seconds. (a truly horrible and non-scalable solution I inherited).

My goal is to use node as a messaging layer to send commands from php to the flash client over a socket.  Flash then would acknowledge receipt of a command and act upon it. In our current world, commands are initiated in php.

I'm looking for a good way to set up bi-directional communication between node and php. From php, I'd need to pass in an ip address and a command.  Node would then find the client with that ip address and send the command to it.

Once I have this up and running I may be able to convince my cohorts to begin building more server code in node.

Thanks for your help,

Steven

codepilot Account

unread,
Apr 30, 2012, 4:03:08 PM4/30/12
to nod...@googlegroups.com
The best way is to write a PHP interpreter in NODEJS, or second best, a NODEJS interpreter in PHP. That is how I would do it!

The preceding comment was a joke, in case that was not obvious.

Mark Hahn

unread,
Apr 30, 2012, 4:29:40 PM4/30/12
to nod...@googlegroups.com
My intent is not to start a religious debate.  

I'm sorry if it appeared that I was arguing for node over PHP.  I wasn't.  I was just pointing out something that is a misconception about node.  I didn't want FUD spread.

No need to apologize for using PHP.  It has been the number one web language for some time.

Chris Rhoden

unread,
Apr 30, 2012, 4:35:59 PM4/30/12
to nod...@googlegroups.com
Dude, this ain't fud. Node's primary strength is that blocking libraries are shunned. You can tell me about how you made a thing that makes it easier to write code with non-blocking libraries, but the thing you're talking about falls down pretty quickly.

And when you talk about writing doTask1, you're building and returning a closure. 1) the code you proposed, as written, does not and will never work because task2 must be defined before it is passed to the function above it and 2) it is simply hiding the exact same complexity I talked about earlier, in a different file.

I absolutely love using node for things it's good at, but please please please please stop this nonsense. You wouldn't write a compiler with Python's Twisted or Ruby's EventMachine.

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en



--
chrisrhoden

Mark Hahn

unread,
Apr 30, 2012, 4:44:35 PM4/30/12
to nod...@googlegroups.com
 task2 must be defined before it is passed to the function above 

Minor correction.  That is not true.  I write and use this code style all the time.  Please note that those are both defined before anything is run.  There is an implied task1() statement somewhere else in the code that starts it running.

As far as the async problem in node, you don't seem to be able to separate the concept of tasks and callbacks.  Yes writing code with callbacks is more of a pain than sync code, but don't spread any FUD about node being less appropriate for sync tasks.


Chris Rhoden

unread,
Apr 30, 2012, 4:50:36 PM4/30/12
to nod...@googlegroups.com
Perhaps this is an issue of language? Can you tell me what you're defining task as? I am referring to a unit of work and you seem to have a much more specific idea of what constitutes a task in mind.

On Mon, Apr 30, 2012 at 4:44 PM, Mark Hahn <ma...@hahnca.com> wrote:
 task2 must be defined before it is passed to the function above 

Minor correction.  That is not true.  I write and use this code style all the time.  Please note that those are both defined before anything is run.  There is an implied task1() statement somewhere else in the code that starts it running.

var callback
undefined
setTimeout(callback, 10000)
9653
callback = function() { console.log("OK") }

Are you telling me that callback will ever run? 
 

As far as the async problem in node, you don't seem to be able to separate the concept of tasks and callbacks.  Yes writing code with callbacks is more of a pain than sync code, but don't spread any FUD about node being less appropriate for sync tasks.

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en



--
chrisrhoden

steven loe

unread,
Apr 30, 2012, 4:58:24 PM4/30/12
to nod...@googlegroups.com
Holy ThreadJack Batman.  "Best way to send data between php and Node" was my question.

Mark Hahn

unread,
Apr 30, 2012, 4:58:56 PM4/30/12
to nod...@googlegroups.com
>   Can you tell me what you're defining task as? 

You said you had two reasons for PHP and that one reason was that you had sequential tasks to perform.  I assumed you meant that your code had to do one thing and then another.  These "things" you have to do are tasks.  I was pointing out that node is just as capable as any other language at doing "things" in order.  If you were actually saying that node is not as good at this, then that is FUD.

Your code won't work but mine will.  If it makes you happier i''ll add a third line to make it clear how it works.

task1 = doTask1 args, task2
task2 = doTask2 args, done
task1()

If you don't understand how that works let me know.

Chris Rhoden

unread,
Apr 30, 2012, 4:59:30 PM4/30/12
to nod...@googlegroups.com
And I stand by my initial response, which was 0mq

On Mon, Apr 30, 2012 at 4:58 PM, steven loe <steve...@gmail.com> wrote:
Holy ThreadJack Batman.  "Best way to send data between php and Node" was my question.

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en



--
chrisrhoden

Rajesh Kumar Jha

unread,
Apr 30, 2012, 1:12:13 PM4/30/12
to nod...@googlegroups.com
Yes Node is ok for that similarly I am using Node and Magento.

On Mon, Apr 30, 2012 at 10:36 PM, steven loe <steve...@gmail.com> wrote:
What's the best technique for sending small amounts of data back and forth between node and php on the same server? Is dNode the best solution for this? Or should I be looking at alternatives else?

Thank you!

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en



--
With regards
Er. Rajesh Kumar Jha
Software Engineer
mail : rkjha.it.in@gmail.com
Ph : +91-9560034877,7428444177

Mark Hahn

unread,
Apr 30, 2012, 5:02:33 PM4/30/12
to nod...@googlegroups.com
And I stand by my initial response, which was 0mq 

Yes that is probably the best way to go.  I wasn't commenting about the original post.  Just clarifying something said.

I'm sorry if I seemed argumentative, I just thought that was an important enough point to hammer home.

Oliver Leics

unread,
Apr 30, 2012, 5:55:02 PM4/30/12
to nod...@googlegroups.com
Oh please.
Please please PLEASE!

Could you guys just stop HIGHJACKING THREADS and show some RESPECT to
the thread starter and the question within the OP?

This is a MailingList and not a chatroom.

Thank you.
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en



--
Oliver Leics @ G+
https://plus.google.com/112912441146721682527

Mark Hahn

unread,
Apr 30, 2012, 5:57:37 PM4/30/12
to nod...@googlegroups.com
Could you guys just stop HIGHJACKING THREADS and show some RESPECT to
the thread starter and the question within the OP? 

Show some respect.  No need to yell.  This sub-thread had some relation to the original post.  It was about PHP and node.

Chris Rhoden

unread,
Apr 30, 2012, 5:59:20 PM4/30/12
to nod...@googlegroups.com
On Mon, Apr 30, 2012 at 5:55 PM, Oliver Leics <oliver...@gmail.com> wrote:
Oh please.
Please please PLEASE!

Could you guys just stop HIGHJACKING THREADS and show some RESPECT to
the thread starter and the question within the OP?

This is a MailingList and not a chatroom.

Once again, I stand by my initial response which was to use a shared datastore or, in cases such as the OP described, a message queue. 


And sorry that I participated, I am just super sick of, "Why would you ever want to do that?" followed by no indication that you've actually tried to think about why someone might want to do that.



--
chrisrhoden

Oliver Leics

unread,
Apr 30, 2012, 6:14:07 PM4/30/12
to nod...@googlegroups.com
Continuing the OT:
There are no sub-threads on mailinglists, just one thread and another
thread. Clear and simple. Separated. Keeps the stuff organized and the
noise low.

The starter of this thread does not want to know about how good or bad
or anything php is against node or whatever.

And I would not yell without having a reason.

Mark Hahn

unread,
Apr 30, 2012, 6:16:34 PM4/30/12
to nod...@googlegroups.com
We will just need to disagree.  Sometimes it is better to stay in a thread and only bother a few people than create a new thread and bother everyone.  Especially when you think it is just going to be one reply and you think each post is going to be the last.

And there is NEVER a reason to yell.

Oliver Leics

unread,
Apr 30, 2012, 6:32:07 PM4/30/12
to nod...@googlegroups.com
Continuing OT:

So, you really want to discuss this, right?
Nah, not with me. I made my point.

Marak Squires

unread,
Apr 30, 2012, 6:38:51 PM4/30/12
to nod...@googlegroups.com
https://github.com/bergie/dnode-php

Try that!

On Mon, Apr 30, 2012 at 10:06 AM, steven loe <steve...@gmail.com> wrote:
What's the best technique for sending small amounts of data back and forth between node and php on the same server? Is dNode the best solution for this? Or should I be looking at alternatives else?

Thank you!

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en



--
-- 
Marak Squires
Co-founder and Chief Evangelist
Nodejitsu, Inc.

Sean Ouimet

unread,
May 1, 2012, 10:35:53 AM5/1/12
to nod...@googlegroups.com
This may not be what you need exactly, but simple HTTP communication could work as well. Your PHP app could do a simple curl request to a local path provided by your node process. Any data needed to be passed between could be sent with the request.

Once the PHP process got a 200 / {started: true} type response from your node app, the PHP script could carry on with its business (say, serving a response to your user) while the Node app continues the work given to it asynchronously. 

(We run numerous microapps based on Node that are (in part) consumed by our legacy apps. Our PHP applications utilize these microapps for data read/writes in a similar way as it would ask MySQL or Memcached for data. Sure HTTP has more overhead than some other options we could use, but it allows us to have specialized servers serving the node processes in a way that is scalable at a different rate than the primary apps, and the APIs are reusable across multiple services.)

On Monday, April 30, 2012 11:06:50 AM UTC-6, sparky wrote:
What's the best technique for sending small amounts of data back and forth between node and php on the same server? Is dNode the best solution for this? Or should I be looking at alternatives else?

Thank you!

Michael Wulf

unread,
May 1, 2012, 6:14:27 PM5/1/12
to nod...@googlegroups.com
Alright, well let's throw out the choices with some sort of comparison. 

Since this is a topic of interest to me, I'll be the martyr and put my thoughts on the table first. Surely more apt heavyweights will offer some corrections (thanks in advance).

It seems to me the options are a message queue, a socket (TCP?), http requests, pipe at the command line (shell script?), send an email, or some sort of third party middleware solution (i.e. a great big, gigantic queue).

A Message Queue

An elegant approach, in my opinion. It would be critical to find a queue where your destination (queue workers) are on call, and not simply checking the queue on some time interval, if you want timely results (given your constraints):

Advantages: Plenty of queue systems available (Amazon SNS, zeromq, nowjs, et al), allows processes to move at their own speed and resources, easy to scale. Easy to move off-server later without sizable changes.

Drawbacks: You need an implementation written in both PHP and Node to serve/handle requests (yay dNode!). Third party libs are almost required. May not adapt well to legacy code that isn't modular.

A Socket

Advantages: A straightforward approach. Just open a socket and keep it available so that two services can talk back and forth. Probably a day project to have up and running. For one-to-one connections, a great answer.

Drawbacks: Not easily scalable for many-to-one or many-to-many. Dropped connections and networking errors could be annoying in PHP unless you have a third party lib to assist. May not adapt well to legacy code that isn't modular.

HTTP Requests

Advantages: Dead simple. This is most likely how your legacy system already accepts requests if it's working with flash. Curl away! Easily scalable and modular, by adding servers behind a load balancer, assuming there's no SQL database involved.

Disadvantages: No pushes. Unless you're using an nginx or node http server, there is a sizable overhead to each request. Even with a single-threaded async server, this is probably more overhead than a queue or socket.

A Pipe / Command Line Script

Advantages: Assuming the destination is a script run on demand, there is a place to drop incoming data in order for it to be picked up, or that there is an access point in the destination process, this could be a very quick way to get things hooked up. Should adapt well to legacy code that is difficult to modify as the shell script can simply pretend to be flash sending in a request and move on. The "script" doing the piping could be JavaScript or PHP itself.

Disadvantages: No immediate answer from destination. Script needs to "call back" somehow. How are errors handled? Not well, probably.

And where does the data get stuffed? In a database? A file? It essentially becomes a queue, waiting for the running threads to check some data store, or the process must hold a socket open for the script to talk to, so perhaps this is no different than using a socket and overly complicated depending on the use case.

Send an Email

Advantages: A bit of an odd solution, and certainly a hack. But simple and straightforward to get up in a jiffy. The email is sent by one process, and either the mail server pipes the message to a script or the destination checks the account and processes.

Disadvantages: A bit convoluted, comes with all the baggage of email--connections, latency, spam/virus filters, poor standards--probably not applicable for anything more complex than a simple text message as the data is likely to get encoded/unencoded several times and be unreliably altered. But hey, it's easy.

Middleware Solution

Amazon SNS (mentioned above) and message queue services like this are essentially middleware of a sort. Here I mention it with the intention of a more traditional solution, such as third party software.

Advantages: this is essentially software that you will drop between the two. Can provide very robust, scalable, and complex connectivity. Can be used to filter/cache/simplify/adapt data between legacy systems.

Disadvantages: Difficult and time consuming to implement, usually costly. Way beyond the scope of your needs.

Isaac Schlueter

unread,
May 6, 2012, 2:54:38 PM5/6/12
to nod...@googlegroups.com
Michael, this is a really helpful response. Thanks.

I think it's important to realize that yes, node certainly *can* do
pretty much all the work that you would do with PHP. But, that
doesn't mean someone has the time or inclination to rewrite some big
app that's *already* in PHP. There may be benefits to doing so, but
it's certainly also got a lot of cost.

In the real world, we often don't have the luxury of starting over.

Alex Kocharin

unread,
May 18, 2013, 12:16:01 PM5/18/13
to nod...@googlegroups.com

PHP is designed to be ran under some apachy stuff and live for just one request. So it would indeed be simpler to keep a node.js server constantly polling server with php module in it with ajax requests.

But if this system will ever eat a lot of resources and you would like to turn it to a long-running daemon... I'd suggest to stop there and rewrite anything to node.js (or python or erlang or go or whatever). Because there're some things that PHP will never do good.


On Saturday, May 18, 2013 3:05:43 PM UTC+4, jasonl...@gmail.com wrote:
HTTP requests would likely be best because most existing PHP software is designed around the HTTP request/responce model. And yes, Node vs '...' aguments are mute when you must use existing  software, especially if your working on existing production systems.
Reply all
Reply to author
Forward
0 new messages