Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending data from php to nodejs server

Is there any way to send string or Json data from php to nodeJS server.

My project use php(Nginx server serves it). And nodeJs server(8080 port, NowJS) is running as background for realtime Pub/Sub update.

When a user write a message, database(Mysql) also is updatd using php. And the change have to send to nodeJS(NowJS, 8080 port) server for realtime updating.

Realtime Update Mechanism : user writes -> php save it in mysql db -> php send info to nodeJS -> nodeJS send the change all subscribers -> others can notice it in realtime.

So my question is, Is there any way to send the changes from php to NodeJS server? I am a nodeJS newbie. Any comments welcomes~ ^^;

like image 725
Jinbom Heo Avatar asked Jun 14 '26 08:06

Jinbom Heo


2 Answers

Any reason you can't use curl?

$ch = curl_init('http://yournodehost/path');
curl_exec($ch);

Depending on the requirements of your request you can set the appropriate curl options.

  • https://www.php.net/curl_init
  • https://www.php.net/curl_setopt
like image 104
jmathai Avatar answered Jun 16 '26 08:06

jmathai


Node.js is pretty flexible, but if you've implemented an http server with it, then your PHP can use file_get_contents to fetch a URL. http://php.net/manual/en/function.file-get-contents.php . This is only for a GET method, you'll have to encode your json appropriately...

As a side note, you might want to have your server modify the database, instead of mixing the functionality half in the PHP and half in the node.js server. That might help keep all the related work in one place, possibly easier to maintain, debug, expand, and so on.

like image 37
david van brink Avatar answered Jun 16 '26 06:06

david van brink