Using Dnode to communicate between PHP and Nodejs

Source: Internet
Author: User
Tags autoload node server

One, install Dnode,

1, for Nodejs, execute

$ sudo npm install dnode


2, for PHP, using composer to install Dnode PHP

Execute the following statement to download composer

Create a file Composer.json, and then fill in the following statement,

{"Require": {"Dnode/dnode": "0.2.0"}}
Execute the following statement installation,

$ sudo php Composer.phar Install

Second, create a simple server program with Nodejs, Server.js

var Dnode = require (' Dnode '); var server = Dnode ({zing:function (n, CB) {CB (n *)}}); Server.listen (7070);
Third, the use of PHP to create a client program client.php, which need to refer to the Dnode folder just installed inside the file autoload.php

<?php//Connect to Dnode server running in Port 7070 and call//Zing with Argument 33require ' Lib/vendor/autoload. PHP ';//This was the class we ' re exposing to dnodeclass temp{//Compute The client ' s temperature and stuff that value I  Nto the callback public function temperature ($CB) {}} $loop = new React\eventloop\streamselectloop (); $dnode = new Dnode\dnode ($loop, New Temp ()); $dnode->connect (7070, function ($remote, $connection) {//Remote is a proxy object T Hat provides us all methods//from the server $remote->zing (for function ($n) use ($connection) {echo "n        = {$n}\n ";    Once we have the result we can close the connection $connection->end (); });}); $loop->run ();? >

Four, perform server-side

$ node Server.js

Five, execute client Invoke server-side program

$ PHP client.php

This invokes the server-side add program and outputs the result

n = 3300



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.