Using easyrtc's room occupant listener, how do I call other users as soon as the user list is updated?

StackOverflow https://stackoverflow.com/questions/23552756

Question

I'm using the room occupant listener to listen for updates on the users that are in the room. In the demos, the user list gets updated with the new users and they have buttons for calling each user, similar to the contact list in an instant messaging program. What I want to do is a conference call, where whenever a user connects, they're automatically called by everyone else.

Here's how my code currently looks like:

var calledUsers = {};
easyrtc.setRoomOccupantListener(function (roomName, userList, selfInfo) {
    for (easyrtcid in userList) {
        if (easyrtcid in calledUsers && calledUsers[easyrtcid]) {
            console.log('already in call with', easyrtcid);
        } else {
            easyrtc.call(
                easyrtcid,
                function success(otherCaller, mediaType) {
                    calledUsers[easyrtc] = true;
                },
                function failure(errorCode, errorMessage) {
                    calledUsers[easyrtc] = false;
                }
            );
        }
    }
});

I tested this in Chrome and Firefox on desktop and mobile.

Am I using the correct listener? Is there some other way to automatically initiate calls with peers when a new user enters the room?

Was it helpful?

Solution

Instead of trying to call any new users in the userList whenever it gets updated, it should be the responsibility of the user just joining the room to call everyone else. In this way you set up a chain:

  1. User X joins the room
  2. User Y joins the room
    1. User list is updated for user X
    2. User Y calls user X
  3. User Z joins the room:
    1. User list is updated for users X and Y
    2. User Z calls user X
    3. User Z calls user Y

And so on. The way to do this is by keeping track of when the user first enters the room.

When entering a room, the event listeners are executed in the following order:

  1. room entry listener
  2. room occupant listener

For other users who are already in the room, only the room occupant listener will be executed.

This means that when you enter a room, you get the current user list and then you can loop through and call each user. But the second time you get an updated user list, in the room occupant listener, you do not call any other user.

The solution for calling other users whenever the user list is updated is:

var needToCallOtherUsers;

easyrtc.setRoomEntryListener(function(entry, roomName) {
    needToCallOtherUsers = true;
});

easyrtc.setRoomOccupantListener(function(roomName, userList, selfInfo) {
    var easyrtcid;
    if (needToCallOtherUsers) {
        for (easyrtcid in userList) {
            easyrtc.call(
                easyrtcid,
                function success(otherCaller, mediaType) {
                    console.log('success: ', otherCaller, mediaType);
                },
                function failure(errorCode, errorMessage) {
                    console.log('failure: ', errorCode, errorMessage);
                }
            );
        }
        needToCallOtherUsers = false;
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top