3

I have two servers, one for the back end app, and one that serves the front end. They are abstracted, but share the same database, I have a need for both to communicate real time events between each other using socket.io.

Front end

// serves a front end website
var appPort  = 9200; 

var express  = require('express');
var app      = express();
var http     = require('http');
var server   = http.createServer(app);
var io       = require('socket.io').listen(server);

io.sockets.on('connection', function(socket) {

    socket.on('createRoom', function(room) {
        socket.join(room); // use this to create a room for each socket (room) is from client side
    });

    socket.on('messageFromClient', function(data) {
        console.log(data)
        socket.broadcast.to(data.chatRoom).emit('messageFromServer', data);
    });


});

Back end

//Serves a back end app
var appPort  = 3100; 

var express  = require('express');
var app      = express();
var http     = require('http');
var server   = http.createServer(app);
var io       = require('socket.io').listen(server);

io.sockets.on('connection', function(socket) {

    socket.on('createRoom', function(room) {
        socket.join(room); // use this to create a room for each socket (room) is from client side
    });

    socket.on('messageFromClient', function(data) {
        console.log(data)
        socket.broadcast.to(data.chatRoom).emit('messageFromServer', data);
    });


});

As an admin I want to log in to my back end where I can see all the people logged in, there I can click on whom I would like to chat with.

Say they are logged in to the front end website, when the admin submits a message client side they trigger this socket.emit('messageFromClient', Message); how can I trigger messageFromClient on the front end using port 9200 submitting from the backend port 3100?

Michael Joseph Aubry
  • 12,282
  • 16
  • 70
  • 135
  • how about using redis for communication between two socket.io instances? [Redis-socket.io](https://www.npmjs.com/package/socket.io-redis) – Arjun Jan 15 '15 at 03:31
  • Someone suggested that, I want to do it, but before I pursue the idea I would like to understand why it would be beneficial. Could you post an answer demonstrating what role the redis server would play. I am confused where the redis server should be hosted and deployed. I will read the link you provided and study it as closely as I can. – Michael Joseph Aubry Jan 15 '15 at 04:00
  • I came across that npm module before, but same thing do I need to setup a third node server, or do I install it on one of my existing servers? I need a better understanding what role it plays. I am sure this is the best solution though. – Michael Joseph Aubry Jan 15 '15 at 04:02
  • 1
    Redis is primarily used to sync the message(events) to multiple servers, so that if you have multiple nodejs servers you can connect it using redis-socketio module and once message is received in any one of the server , the redis will send this message to all other connected servers. Redis can be installed in the same server where application is running. [More about redis usage and deployment](http://stackoverflow.com/questions/9267292/examples-in-using-redisstore-in-socket-io) – Arjun Jan 15 '15 at 06:42
  • Thanks for the explanation, I have been reading about it. I was just skeptical in using it because I didn't want to setup a third server. – Michael Joseph Aubry Jan 15 '15 at 19:25

1 Answers1

8

You really dont need to start the socket.io server in the front end for this use case.

The way you can get it to work is :

  1. Keep the backend as it is, which acts as a Socket.IO server
  2. In the font end connect to the server using a Socket.IO client.

You can install the client by calling

npm install socket.io-client

and then connect to the server using :

var io = require('socket.io-client'),
socket = io.connect('localhost', {
    port: 3100
});
socket.on('connect', function () { console.log("socket connected"); });
socket.emit('messageFromClient', { user: 'someuser1', msg: 'i am online' });

You can then create a map of socket objects with their corresponding username and send a message to that user based on your business logic.

More Information :

You may have to add cases like client disconnection etc. You can read more about the call backs for that here :

https://github.com/Automattic/socket.io-client

Pushkar
  • 7,450
  • 10
  • 38
  • 57
  • Ah nice man just in the nick of time. I actually did try it this way (removing socket.io from the front end), I included the socket.io script for the backend on the front end, but it still was not working. I forgot about `npm install socket.io-client` I will use this and it should be good to go whew. – Michael Joseph Aubry Jan 15 '15 at 19:23
  • I am having a hard time configuring this lol, but I just thought how will the socket.io server :3100 communicate with :9200 front end? – Michael Joseph Aubry Jan 15 '15 at 21:53
  • I solved the issue by using `var socket = io.connect('http://localhost:3100');` instead of `var socket = io('http://localhost:3100');` which the docs say to use, what the heck it sent me on a wild goose chase, but it works with `connect` which I though got depreciated a while back? – Michael Joseph Aubry Jan 16 '15 at 05:31