/ node

Perl, PHP and socket.io

for perl there is no socket.io library, for php there are a few including the php-emitter.
For me nothing worked really well, so i decided to communicate with my socket.io server the unix way.
in production i use php -> node socket.io client -> node socket.io server
where php and the socket.io client running on the same server.
it run's very well and serves over 500.000 users monthly.

here is my node.js script to open a unix socket.
Every line the socket gets will be emitted

var net = require('net');
var fs = require('fs');

//do the connect
var io = require('socket.io-client').connect(
        'http://other_server', {
        port: 80

if (fs.existsSync('/tmp/node_socket')) {

var server = net.createServer(function(con) {
    con.on('data', function(data) {
        if(typeof data != 'undefined' && data != null)
            var str = data.toString();
            var arr = str.split("\n");
            arr.forEach(function(string) {
                 if ( string ) {
                     console.log('Data received by socket: ' + string);
                     io.emit('something', JSON.parse(string));




in php i simply do

$sock = stream_socket_client('unix:///tmp/node_socket', $errno, $errstr);
fwrite($sock, 'SOME string'."\n");
Perl, PHP and socket.io
Share this