Presence Detection

muaz-khan edited this page Oct 10, 2014 · 1 revision

This wiki page is added to explain how to detect presence of big-rooms i.e. "channels" and how to detect presence of small-rooms i.e. "sessions".

Firebase

Firebase is a 3rd party service providing free signaling tiers, which is handy for individuals and getting starters. Here is how to detect presence of a big-room i.e. presence of a channel:

var firebaseURI = connection.resources.firebaseio + connection.channel;
new window.Firebase(firebaseURI).once('value', function(data) {
    var isRoomPresent = data.val() != null;
    if (!isRoomPresent) {
        connection.open(connection.sessionid);
    } else {
        connection.join(connection.sessionid);
    }
});

Here is how to store small-room i.e. session in Firebase servers, and how to get such values when required:

var firebaseURI = connection.resources.firebaseio + 'sessionid/' + connection.sessionid;
var firebase = new window.Firebase(firebaseURI);

btnOpenSmallRoom.onclick = function() {
    var smallRoomId = 'something';
    connection.sessionid = smallRoomId;
    connection.open();

    // storing small room over firebase servers
    firebase.set(smallRoomId);
};

firebase.once('value', function(data) {
    var isSmallRoomPresent = data.val() != null;
    if (!isSmallRoomPresent) {
        btnOpenSmallRoom.disabled = false;
        btnOpenSmallRoom.onclick();
    } else {
        var smallRoomid = data.val();
        connection.join(smallRoomid);
    }
});

Socket.io over Node.js

Source code here:

Here is how to detect presence of a big-room i.e. channel:

var socket = io.connect('/');

socket.on('presence', function (isChannelPresent) {
    if (!isChannelPresent)
        connection.opne(connection.sessionid);
    else
        connection.join(connection.sessionid);
});

socket.emit('presence', channel);

Ref:

Current socketio-over-nodejs implementation doesn't support detection of small-rooms i.e. sessions however this feature can easily be added there.

WebSocket over Node.js

Source code here:

Here is how to detect presence of a big-room i.e. channel:

var SIGNALING_SERVER = 'wss://wsnodejs.nodejitsu.com:443';
var websocket = new WebSocket(SIGNALING_SERVER);

websocket.onmessage = function (event) {
    var data = JSON.parse(event.data);

    if (data.isChannelPresent == false) {
        connection.open(connection.sessionid);
    } else {
        connection.join(connection.sessionid);
    }
};

websocket.onopen = function () {
    websocket.send(JSON.stringify({
        checkPresence: true,
        channel: connection.channel
    }));
};

Current websocket-over-nodejs implementation doesn't support detection of small-rooms i.e. sessions however this feature can easily be added there.

Small Rooms i.e. Sessions

You can detect presence of small-rooms even if an implementation isn't supporting this feature.

connection.onCustomMessage = function(message) {
    if (message['anyone-open-this-room']) {
        var roomid = message['anyone-open-this-room'];
        if (connection.isInitiator && connection.sessionid == roomid) {
            connection.sendCustomMessage({
                'yes-i-open-this-room': roomid
            });
        }
    }

    if (message['yes-i-open-this-room']) {
        // var roomid = message['yes-i-open-this-room'];
        isSmallRoomOpened = true;
    }
};

btnCheckPresenceOfSmallRoom.onclick = function() {
    connection.sendCustomMessage({
        'anyone-open-this-room': 'small-room-id'
    });

    // wait 3 seconds for response
    setTimeout(openSmallRoom, 3 * 1000);
};

var isSmallRoomOpened = false;

function openSmallRoom() {
    if (isSmallRoomOpened) {
        alert('Small room is already opened.');
        return;
    }

    connection.sessionid = 'small-room-id';
    connection.open();
}