Custom Messages

Damilola Emmanuel Olowookere edited this page Sep 4, 2017 · 2 revisions

RTCMultiConnection is supporting different methods and evens for exchanging custom messages between two or more users, publicly or securely.

This wiki page explains all possible methods to exchange custom data among different set of users.

connection.sendCustomMessage

Using this method, you can share any kind of data "publicly" among all users who are part of current "big-room" i.e. current "channel". For example, you can synchronize session-ids, or room-names, or initiator-ids or any kind of data that needed to be exchanged among all connected or non-connected users:

// first step is to listen for event handler
connection.onCustomMessage = function(message) {
   if(message.roomNameChanged == true) {
       connection.sessionDescriptions[message.sessionid] = message.newRoomName;
   }

   if(message.initiatorAbsent == true) {
       alert('Please wait a few minutes until room moderator come back');
   }

   if(message.userOnline == true) {
       var image = document.getElementById('img-' + message.userid);
       image.src = 'user-online.png';
   }
}

Here is how to send public message to all connected users in a channel:

connection.sendCustomMessage({
    userOnline: true,
    userid: connection.userid
});

Difference between "connected-to-channel" and "connected-to-session"

RTCMultiConnection is having a concept named as "default-socket" where a public socket is opened and used in following situations:

  1. To share room-details i.e. who created room, room-id and so on. (that's why onNewSession event is fired)
  2. To send participation requests to moderator (that's why onRequest event is fired)
  3. To do an action among all users that are part of current channel

So, here is difference:

  1. When a user is connected to merely default socket; he is considered as "connected-to-channel"
  2. When a user is also connected to "all-participants" including "moderator" then it is considered as "connected-to-session"

Following three methods connects-to-channel i.e. opens default socket:

  1. open
  2. connect
  3. join

But, here are only two methods that connects-to-session i.e. users are connected with each other:

  1. join
  2. accept

connection.join method can be either directly used or it can be used inside onNewSession event:

connection.onNewSession = function(session) {
   connection.join(session);
};

And connection.accept can only be used inside onRequest event:

connection.onRequest = function(request) {
   connection.accept(request);
};

connection.peers['remote-userid'].sendCustomMessage

Here is how to send private messages between two users only:

// first of all, listen for event handler
connection.onconnected = function(event) {
    connection.peers[event.userid].onCustomMessage = function(message) {
        if (message.shareScreen == true) {
            connection.addStream({
                screen: true,
                oneway: true
            });
        }

        if (message.getOutOfRoom) {
            connection.leave();
            connection.disconnect();
        }
    };
};

Here is how to send private message:

connection.peers['remote-userid'].sendCustomMessage({
    getOutOfRoom: true
});

Send custom messages using "external" sockets

This page explains how to use single-socket for RTCMultiConnection signaling:

Here is an example:

var onMessageCallbacks = {};
var socketio = io.connect('http://localhost:8888/');

socketio.on('message', function(data) {
    if (data.sender == connection.userid) return;

    if (data.isRMCPriateMessage == true && onMessageCallbacks[data.channel]) {
        onMessageCallbacks[data.channel](data.message);
    };

    // -------------------- this this if-block:
    if (data.isRMCPriateMessage == false) {
        // your custom messages goes here

        if (data.getOutOfRoom == true && data.messageFor == connection.userid) {
            connection.leave();
            connection.disconnect();
        }

        if (data.broadcastYourVideo == true && data.messageFor == connection.userid) {
            connection.addStream({
                video: true,
                oneway: true
            });
        }
    }
});

connection.openSignalingChannel = function(config) {
    var channel = config.channel || this.channel;
    onMessageCallbacks[channel] = config.onmessage;

    if (config.onopen) setTimeout(config.onopen, 1000);
    return {
        send: function(message) {
            socketio.emit('message', {
                sender: connection.userid,
                channel: channel,
                message: message,
                isRMCPriateMessage: true // -------- check this line
            });
        },
        channel: channel
    };
};

isRMCPriateMessage is a boolean that is set inside openSignalingChannel method. See above snippet.

Here is how to send custom messages:

// send string
socketio.emit('message', 'your-simple-custom-message');

// send an object
socketio.emit('message', {
    getOutOfRoom: true,
    messageFor: 'remote-userid'
});