Manage multiple tabs (but same user) in socket.io

后端 未结 4 966
天涯浪人
天涯浪人 2020-12-02 16:15

I\'m having some problems with socket.io, and I have no idea how to solve it. I\'ve an app with a login system with socket.io to manage user interactions. Also I\'ve an arra

4条回答
  •  难免孤独
    2020-12-02 16:56

    Intercom library can be used to share and manage a socket.io connection.

    // Initialize socket.io on one tab
    var socket = io.connect('//yourwebsite.com/socketio');
    
    // Initialize intercom on all tab
    var intercom = Intercom.getInstance();
    intercom.bind(socket);
    
    // Listen to an event
    intercom.on('msgBroadcast', function(data){
        console.log(data);
    });
    
    // Emit an event
    intercom.emit('msgBroadcast', {
        socket: true,
        message: 'Hello all!'
    });
    

    You could also use socketio shared webworkers

提交回复
热议问题