RTCMultiConnection Docs

RTCMultiConnection API Reference / "connect" method

"connect" method can be used to connect to socket; which is known as signaling channel in WebRTC. That socket will be used for presence detections and initial handshake.

"connect" method can be invoked both for initiator and participants. However it is usually recommended to be invoked only for participants. Read more at the bottom of this page.

How to invoke "connect" method?

connection.connect('session-id');

// you can restrict its usage only for participants
if(your_are_participant) {
    connection.connect();
}

Quick Demo: connect is invoked both for participants and initiator

<script src="//www.rtcmulticonnection.org/latest.js"></script>
<button id="openNewSessionButton">Open New Room</button>
<script>
var connection = new RTCMultiConnection().connect();
document.querySelector('#openNewSessionButton').onclick = function() {
    connection.open();
};
</script>

Another Demo: connect is invoked only for participants

<script src="//www.rtcmulticonnection.org/latest.js"></script>
<button id="openNewSessionButton">Open New Room</button>
<button id="joinSessionButton">Join Room</button>
<script>
var connection = new RTCMultiConnection();
document.querySelector('#openNewSessionButton').onclick = function() {
    connection.open();
};
document.querySelector('#joinSessionButton').onclick = function() {
    connection.connect();
};
</script>

FAQ

Why you're using "connect" method both for initiator and participants?

Answer

  1. "connect" method connects to signaling channel. It doesn't tries to "join" or "open" the room.
  2. "open" method quickly opens the room....so participants shouldn't use it.
  3. Without using "connect" method, you can NEVER get "onNewSession" event to be fired. Though you can use "join" method directly.

Remember, if you're using "join" method directly, then keep in mind that "join" method quickly joins the room....so initiator shouldn't use it.

Summary: Usually it is a good practice to use "connect" method only for participants however feel free to use it for initiator as well. RTCMultiConnection is smart enough to understand which property/method/object is useful for which user.
What if I use both 'connect' and 'open' methods for initiator? How it will work?

Answer

"connect" method will simply initialize signaling medium. Now, we've an active signaling gateway and we're ready to use it for presence detection and signaling. Later on, as soon as you'll invoke "open" method; RTCMultiConnection will detect that signaling medium is already initialized; it will NEVER try again to initialize the signaling channel. It will simply use existing opened channel and "create" the room.

To understand it from coding perspective, RTCMultiConnection has a "RTCMultiSession" object which is usually known as "backbone" object because it plays middleman role between public-API and the rest of all private/or RTCPeerConnection API.

This backbone object i.e. "RTCMultiSession" handles signaling channels as soon as its constructor is invoked/initialized.

It means that if you called "connect" method; its constructor is initialized like this:

var rtcMultiSession;

connection.connect = function() {
    if(rtcMultiSession == 'undefined') {
        // it will auto connect to signaling channel
        rtcMultiSession = new RTCMultiSession();
    }
};

Now, "open" method will check if it is already initialized i.e. channel is already connected:

connection.open = function() {
    if(rtcMultiSession == 'undefined') {
        // it will auto connect to signaling channel
        rtcMultiSession = new RTCMultiSession();
    }
};
What's difference between connect and join method?

Answer

connect method simply connects to socket. It'll NEVER try to auto join the room. However, onNewSession event will be fired as soon as a room is available.

join connects the socket, then quickly captures user-media and then quickly joins the room. It NEVER waits for onNewSession event to be fired. Also, onNewSession event will NEVER be fired if you're using join method.

One last point for join method; it is possible to pass session-id over join method:

connection.join('session-id');

In this case, when "session-id" is passed over "join" method; RTCMultiConnection will wait for new-room; and as soon as a room gets available; RTCMultiConnection will capture user-media and quickly join the room. As it is said, RTCMultiConnection will NEVER fire "onNewSession" event if "join" method is used.


Want to ask a Question?

You can include your email for private conversation!

Latest Updates