Saturday, 3 June 2017

how to connect nodejs and php server using socketio

How to send data from node server to PHP server(Apache) using Socket.io

Server Side:

var socket = require( 'socket.io' );
var http = require( 'http' );

var io = require('socket.io').listen(9990);

io.sockets.on('connection', function (socket) {
  console.log('user connected!');

Client Side (PHP SERVER, Javascript code):

var socket = io.connect( 'http://localhost:8080' );

$( "#messageForm" ).submit( function() {
    var nameVal = $( "#nameInput" ).val();
    var msg = $( "#messageInput" ).val();
    socket.emit( 'message', { name: nameVal, message: msg } );
    });

    return false;
});

socket.on( 'message', function( data ) {
    var actualContent = $( "#messages" ).html();
    var newMsgContent = '<li> <strong>' + data.name + '</strong> : ' + data.message + '</li>';
    var content = newMsgContent + actualContent;

    $( "#messages" ).html( content );
});

  socket.on('foo', function (data) {
    console.log('here we are in action event and data is: ' + data);
  });
});



via Den Biswajit

No comments:

Post a Comment