最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Socket.io trigger events between two node.js apps? - Stack Overflow

programmeradmin0浏览0评论

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 municate 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?

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 municate 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?

Share Improve this question edited Jan 14, 2015 at 22:12 Michael Joseph Aubry asked Jan 14, 2015 at 22:06 Michael Joseph AubryMichael Joseph Aubry 13.5k16 gold badges77 silver badges140 bronze badges 5
  • how about using redis for munication between two socket.io instances? Redis-socket.io – Arjun Commented Jan 15, 2015 at 3: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 Commented Jan 15, 2015 at 4: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 Commented Jan 15, 2015 at 4: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 – Arjun Commented Jan 15, 2015 at 6: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 Commented Jan 15, 2015 at 19:25
Add a ment  | 

1 Answer 1

Reset to default 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./Automattic/socket.io-client

发布评论

评论列表(0)

  1. 暂无评论