N
N
Nikita Pikovets2016-01-26 14:49:15
PHP
Nikita Pikovets, 2016-01-26 14:49:15

How to connect Node.js and PHP?

Suppose there is a site in php, you need to run a websocket on the node that will respond to events on the client and transfer information to the php server and back, how can this be implemented?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
OVK2015, 2016-01-26
@adrenalinik

Or like this:
node:

var ipaddress = '127.0.0.1';
var port = 9000;
function testWebSocketServerAndNotification()
{
  var WebSocketServer = require('ws').Server;
  var path = require('path');
  var notifier = require('node-notifier');

  var message = 'Hello from Node.js';
  var path2Image = 'e:\\MyWork\\NodeWorkBench\\TestApp\\image\\infoBalloon.png';

  var wss = new WebSocketServer({host:ipaddress, port:port});

  wss.broadcast = function(data) 
  {
    for (var i in this.clients)
    {
      this.clients[i].send(data);
    }
  };

  wss.on('connection', function(ws) 
  {
    ws.on('message', function(message) 
    {
      wss.broadcast(message);
    notifier.notify
    ({
      title: 'My notification',
      message: `Message: '${message}'`,
      sound: true, 
      time: 5000, 
      wait: true, 
      icon: path.join(__dirname, 'image', 'notificationIcon.png'), 
      contentImage: path2Image,
    });    

    });
  });

  console.log('Слушаем адрес ' + ipaddress + ' на порте: ' + port + ' ...');
}

php:
<?php
  require_once "e:\\MyWork\\web\\Script\\PHP\\PHP_SocketIO_Client\\socket.io.php";
  $socketio = new SocketIO();
  echo iconv("UTF-8", "CP866", "exit для выхода\n");
  while(true)
  {
    $input = fgets(STDIN);
    if(trim($input) == "exit")
    {
      break;
    }
    if ($socketio->send("127.0.0.1", 9000, iconv("CP866", "UTF-8", trim($input))))
    {
        echo iconv("UTF-8", "CP866", "Сообщение отправлено. Можете продолжать:\n");
    } else 
    {
        echo iconv("UTF-8", "CP866", "Какой-то сбой :(\n");
    }
  }	
  exit("Jobe done");
?>

socket.io.php took from here https://github.com/psinetron/PHP_SocketIO_Client

D
Dmitry Belyaev, 2016-01-26
@bingo347

Redis (pub/sub), zeroMQ

T
Timur Shemsedinov, 2016-01-26
@MarcusAurelius

It is easiest for these applications to exchange HTTP requests on localhost, but with a high intensity of such requests, or if it is necessary to optimize the response time, this becomes inefficient. Then there are a bunch of other IPC facilities, sockets, mutexes and semaphores, the filesystem, memory-mapped (or shared memory) files, through the database, through the event bus and MQ system message queue (ZeroMQ, ActiveMQ, RabbitMQ, Redis, AMQP, etc. .d.). But all these methods will not greatly improve the situation compared to HTTP, because the PHP application is always terminated and restarted, in general, it does not live long. Each time it is run, it will again establish a connection to either the event bus or the database, or open sockets to a Node.js application that lives in memory for a long time. All of these methods are good for the interaction of two long-lived applications that can establish a connection and use it for a long time, and keep their own memory for a long time, and simply synchronize the state of several processes through the connection. And in the case of PHP, ordinary HTTP requests are enough, you still won’t build a highly loaded system on a bunch of PHP + Node.js, but save time if you make it as simple as possible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question