Search code examples
phpnode.jsapachesocket.iolamp

node.js and apache on different servers


I have node.js and socket.io on server A and lamp on server B. Server B is the website that runs everything that my sites need except I want server A to take care of the chat feature I have on server B.

I'm kinda new to node.js and socket.io, But got the hang of sending and receiving messages on server A with a simple index.html.

My question is... what's the best way or how do you send and receive messages from server B to A and back? so I can keep everything I already wrote on server B and just use server A to serve as the chat server for all the messages?

Thanks.


Solution

  • Javascript on web server A:

    <script src="http://serverB.com/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('http://serverB.com');
      socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
      });
    </script>
    

    NodeJS server B:

    var io = require('socket.io').listen(80);
    
    io.sockets.on('connection', function (socket) {
      socket.emit('news', { hello: 'world' });
      socket.on('my other event', function (data) {
        console.log(data);
      });
    });