Unpublish/unsubscribe a stream

If User B wanted to cease publishing to the session, the Session.unpublish() method would be used as shown below:

User B unpublishes its stream; User A subsequently unsubscribes from User B's stream

The Session.unpublish() method stops the specified publisher from streaming and removes the local video display from the web page.

The session sends a streamDestroyed event to all connections. Assuming an event listener is registered for this event, it will be invoked with a StreamEvent object. This object includes an array of streams that have been destroyed.

Usually, if a web page is subscribing to a stream that gets destroyed, the web page will want to unsubscribe from the stream. The helper method Session.getSubscribersForStream() makes it easy to figure out which subscribers to pass to session.unsubscribe().

User A calls Session.getSubscribersForStream(), passing it the stream contained in the StreamEvent object received through the streamDestroyed event listener. User A can then invoke Session.unsubscribe(), passing it the subscriber returned from the previous call. Calling the Session.unsubscribe() method removes the video display associated with the specified subscriber's stream from the local web page.

Note that User B's web page is also notified of the destruction of the stream they just ceased publishing. Once again, User B's web page can know that a streamEvent references its own previously published stream by comparing the stream.connection.connectionId from the StreamEvent streams array to its own session.connection.connectionId; in most cases, User B's web page will choose to ignore the notification.

Next workflow step: Disconnect from a session.

IRC Live Chat

Have a quick question? Chat with TokBox Support on IRC. Join chat