Socket IO Server to Server

前端 未结 3 1471
时光取名叫无心
时光取名叫无心 2020-12-04 19:37

Is it possible for a server to connect to another using Socket.IO and be treated like a client?

And have it join rooms, recieve io.sockets.in(\'lobby\').emit(). And

相关标签:
3条回答
  • 2020-12-04 20:01

    I had the same problem, but instead to use socket.io-client I decided to use a more simple approach (at least for me) using redis pub/sub, the result is pretty simple.

    You can take a look at my solution here: https://github.com/alissonperez/scalable-socket-io-server

    With this solution you can have how much process/servers you want (using auto-scaling solution), you just use redis as a way to forward your messages between your servers.

    0 讨论(0)
  • 2020-12-04 20:11

    Yes, absolutely. Just use the Socket.IO client in your server application directly.

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

    You can install it with npm install socket.io-client. Then to use:

    var socket = io.connect('http://example.com');
    socket.on('connect', function () {
      // socket connected
      socket.emit('server custom event', { my: 'data' });
    });
    
    0 讨论(0)
  • 2020-12-04 20:17

    I realize this is an old post, but i was working on something similar and decided to come back and contribute something as it got me thinking..

    Here's a basic Client -> Server 1 -> Server 2 setup

    Server #1

    // Server 1
    var io = require("socket.io").listen(8099); // This is the Server for SERVER 1
    var other_server = require("socket.io-client")('http://domain.com:8100'); // This is a client connecting to the SERVER 2
    
    other_server.on("connect",function(){
        other_server.on('message',function(data){
            // We received a message from Server 2
            // We are going to forward/broadcast that message to the "Lobby" room
            io.to('lobby').emit('message',data);
        });
    });
    
    io.sockets.on("connection",function(socket){
        // Display a connected message
        console.log("User-Client Connected!");
    
        // Lets force this connection into the lobby room.
        socket.join('lobby');
    
        // Some roster/user management logic to track them
        // This would be upto you to add :)
    
        // When we receive a message...
        socket.on("message",function(data){
            // We need to just forward this message to our other guy
            // We are literally just forwarding the whole data packet
            other_server.emit("message",data);
        });
    
        socket.on("disconnect",function(data){
            // We need to notify Server 2 that the client has disconnected
            other_server.emit("message","UD,"+socket.id);
    
            // Other logic you may or may not want
            // Your other disconnect code here
        });
    });
    

    And here's Server #2

    // Server 2
    var io = require("socket.io").listen(8100);
    io.sockets.on("connection",function(socket){
        // Display a connected message
        console.log("Server-Client Connected!");
    
        // When we receive a message...
        socket.on("message",function(data){
            // We got a message... I dunno what we should do with this...
        });
    });
    

    This is our Client, who sends the original message.

    // Client
    var socket = io('http://localhost');
    socket.on('connect', function(){
        socket.emit("message","This is my message");
    
        socket.on('message',function(data){
            console.log("We got a message: ",data);
        });
    });
    

    I'm making this post a Community Wiki so that someone can improve this if they feel like it.

    !!THE CODE HAS NOT BEEN TESTED, USE AT YOUR OWN RISK!!

    0 讨论(0)
提交回复
热议问题