Search code examples
node.jsclient-servergame-enginemultiplayerdeepstream.io

Deepstream.io - Server callback when client disconnects?


I'm building a Multiplayer game with Deepstream using an Authorative server model. My server is just another Node.JS client. Is there any way for my server to find out if any of the connected clients disconnected or closed their connection? Is there an event or a callback that is exposed?

I can build a heartbeat system but I wonder if this can be avoided.


Solution

  • Yup, an official "presence" feature is already feature complete and in test. However you can already mimic its functionality via listening as shown in this tutorial at this line of code.

    Generally speaking, all clients would subscribe to a status event specific to them, e.g.

    ds.event.subscribe( 'status/' + name );
    

    The server would now listen for subscriptions for these events and deduce the online status:

    ds.event.listen( 'status/.*', this._playerOnlineStatusChanged.bind( this ) );
    
    _playerOnlineStatusChanged( match, isSubscribed ) {
            // Extract the player name from the status event
            var name = match.replace( 'status/', '' );
    
            if( isSubscribed ) {
                this.addPlayer( name );
            } else {
                this.removePlayer( name );
            }
        }
    

    This will keep track of any disconnect, whether intentional or accidental