3

I'd like to use Socket.io and Node.js to do push notifications. The end goal is to do something similar to what Stackoverflow does with their commenting to notify people of new comments.

My site is in PHP and runs on Apache on an EC2 instance. I've heard that Apache doesn't handle concurrency well so I'm interested in using Node to handle the hopefully large # of simultaneous, persistent connections. I guess there are two solutions:

  1. Put Node on same instance as Apache and proxy the two servers
  2. Put Node on a separate instance

In either case, I don't know how the connection between these two servers looks programmatically. For example, while WebSockets/Node handles the sending/receiving of messages, I need to store these messages in my MySQL database and that would require some PHP code, yes/no? Also, how is the message received on my PHP page? Via a $.post to a url like this, http://mysite/receiver.php that would be resolved by my Apache server?

I'd be happy to see either comments or code to help me understand this better.

tim peterson
  • 23,653
  • 59
  • 177
  • 299
  • 1
    My experience with node.js is admittedly limited but as far as I understand, apples and oranges. A node.js server should not need to communicate with an apache one — there are node.js libraries to handle MySQL connections though, so you can probably do whatever you need to in server-side javascript via node.js. As long as you are confident with JS you should be fine with no PHP involvement. – Mahn Jul 24 '12 at 15:48
  • Also, but again as far as I understand, you should be fine running both node.js and apache on a single instance; I think you can just have a specific port handled by node.js and the usual 80/4430 via apache (someone with more node.js hands on experience may want to correct me on this if it is otherwise though) – Mahn Jul 24 '12 at 15:52
  • @Mahn thanks, can you explain where I specify the port number? Is that part of the URL or is that PHP.ini or Node config code? And I think the Node is port 3000? – tim peterson Jul 24 '12 at 16:02
  • Have a look at @GeoPhoenix answer since it seems he already covered it :) – Mahn Jul 24 '12 at 16:43
  • @timpeterson Were you able to find some code snippet to implement your task? I am searching for the exact problem. – Akash Saikia Apr 30 '14 at 15:29

3 Answers3

4
  • You can run different servers at different ports.
  • In order for the servers to communicate in a way you can use Sessions/MySQL/flat files/Cache/json/xml...
  • You can store messages wherever you want there is no limitation on that, MySQL module for node.js exists, memcache module for node.js exists and many more, PHP code is not required, its optional to query the database.
  • The messages on your PHP page are fetched from your database and rendered, if node.js stores stuff in mysql server you can fetch them with PHP or temporarily store to an array and push messages back to client while you store into database for new users to have "current" content.
  • Avoid using ajax since you can stream data to client via websockets in realtime use when needed.

-- Edited
Some additional information on How to use vhosts alongside node-http-proxy? link provided from Timothy Strimple at comments , thanks

Some MySQL drivers can be found in this question What MySQL drivers are available for node.js?

About configuring ports:
-On apache you need to manually edit httpd.conf to define default port
-On node.js things are simpler and you can define a port in code Using node.js as a simple web server

   var connect = require('connect');
    connect.createServer(
     // .. code
    ).listen(PORT);

hope it helped

Community
  • 1
  • 1
Gntem
  • 6,949
  • 2
  • 35
  • 48
  • -@GeoPhoenix thanks, can you explain what the code looks to run different servers at different ports? Also, do you have a recommendation on how to connect Node to MySQL. The [node-mysql](https://github.com/felixge/node-mysql) link provided by -@NickHagianis seems good, nice and simple. I don't want to learn a new framework, would prefer to just be able to give my credentials (DBuser, DBpassword, DBhost) and make the message `INSERT`s using SQL syntax. – tim peterson Jul 24 '12 at 16:11
  • -@GeoPhoenix can the `PORT` be any number? – tim peterson Jul 24 '12 at 16:31
  • 1
    yes it has to be a number, an integer from 0 to 65535, be carefull not to conflict with any already registered port – Gntem Jul 24 '12 at 16:36
  • -@GeoPhoenix thanks, so on my receiver.php page I would just use the `CLIENT` – tim peterson Jul 24 '12 at 17:34
  • @timpeterson yes for `client` in PHP , just print javascript that creates websocket connection to the server, other than that is up to you on how it will handle responses from server. – Gntem Jul 24 '12 at 19:38
  • Some information on how to run apache and node.js side by side. http://stackoverflow.com/questions/9368794/how-to-use-vhosts-alongside-node-http-proxy – Timothy Strimple Jul 25 '12 at 00:21
2

I need to store these messages in my MySQL database and that would require some PHP code, yes/no?

No. Node allows you to write javavscript on the server and you can do pretty much anything with it that you would do with PHP, including connect to a MySQL database.

You can run both Node and Apache on the same server and use NGINX to proxy between the two, or you could use Node to proxy to your Apache. But don't use Apache to proxy to Node as you'd be hurting performance.

Here are some links that will hopefully help:

Nick Hagianis
  • 1,438
  • 14
  • 20
  • 3
    For the record, there's no need of a middle man, node.js and apache will run just fine side by side, provided they are listening to different ports. – Mahn Jul 27 '12 at 22:46
  • Correct. Didn't bother updating since @GeoPhoenix covered that in detail. – Nick Hagianis Jul 30 '12 at 16:50
1

You could also use sockets as a direct means of communication between the two using node.js's net module and php's socket module. Here is an example.

Simple node js server listening for a connection

var net = require('net');

var listenport = 3000; // Should be the same as in the php script

// Set up server
var Server = net.createServer(function(Sock) {
    console.log('Client Connected.');

    Sock.on('data', function(data) {
       console.log('Data received: ' + data);

       dataobj = JSON.parse(data);

       console.log('Item ID: ' + dataobj.itemid);

       // and so on
    });

    Sock.on('end', function() {
        console.log('Client Disconnected.');
    });

    Sock.pipe(Sock);
});

Server.listen(listenport, function() {
   console.log('Listening on port ' + listenport); 
});

Simple PHP connecting to the node instance

<?php
error_reporting(E_ALL);

$port = 3000; // Port the node app listens to
$address = '127.0.0.1'; // IP the node app is on

// Create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

// Connect to node app
$result = socket_connect($socket, $address, $port);
if ($result === false) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
}

// Data we want to send
$data = array('itemid' => '1234567', 'steamid' => '769591951959', 'otherinfo' => 'hi there');

// Prepares to transmit it
$encdata = json_encode($data);

socket_write($socket, $encdata, strlen($encdata));

socket_close($socket);

echo 'Sent data\n';

?>

original source: github how to connect php to node js

Alex_Nabu
  • 311
  • 3
  • 11