Example — streamCreated event dispatched by the Session object
The following code connects to a session and sets up an event listener for when a stream published by another client is created:
session.on("streamCreated", function(event) {
subscriber = session.subscribe(event.stream, targetElement);
}).connect(token);
Example — streamDestroyed event dispatched by the Session object
The following code connects to a session and sets up an event listener for when other clients' streams end:
session.on("streamDestroyed", function(event) {
console.log("Stream " + event.stream.name + " ended. " + event.reason);
}).connect(token);
Example — streamCreated event dispatched by a Publisher object
The following code publishes a stream and adds an event listener for when the streaming starts
var publisher = session.publish(targetElement)
.on("streamCreated", function(event) {
console.log("Publisher started streaming.");
});
Example — streamDestroyed event dispatched by a Publisher object
The following code publishes a stream, and leaves the Publisher in the HTML DOM when the streaming stops:
var publisher = session.publish(targetElement)
.on("streamDestroyed", function(event) {
event.preventDefault();
console.log("Publisher stopped streaming.");
});
Properties
Name | Type | Description |
---|---|---|
cancelable |
Boolean | Whether the event has a default behavior that is cancelable
(true ) or not (false ). You can cancel the default behavior by
calling the preventDefault() method of the StreamEvent object in the event
listener function. The streamDestroyed event is cancelable.
(See preventDefault().) |
reason |
String | For a streamDestroyed event,
a description of why the stream was destroyed. This property can have one of the following
values:
Depending on the context, this description may allow the developer to refine the course of action they take in response to an event. For a |
stream |
Stream | A Stream object corresponding to the stream that was added (in the
case of a streamCreated event) or deleted (in the case of a
streamDestroyed event). |
streams |
Array | Deprecated. Use the stream property. A
streamCreated or streamDestroyed event is dispatched for
each stream added or destroyed. |
Methods
Name | Description |
---|---|
isDefaultPrevented() → {Boolean} |
Whether the default event behavior has been prevented via a call to
preventDefault() (true ) or not (false ). |
preventDefault() |
Prevents the default behavior associated with the event from taking place. |
isDefaultPrevented() → {Boolean}
preventDefault()
(true
) or not (false
).
See preventDefault().
Returns:
preventDefault()
For the streamDestroyed
event dispatched by the Session object,
the default behavior is that all Subscriber objects that are subscribed to the stream are
unsubscribed and removed from the HTML DOM. Each Subscriber object dispatches a
destroyed
event when the element is removed from the HTML DOM. If you call the
preventDefault()
method in the event listener for the streamDestroyed
event, the default behavior is prevented and you can clean up Subscriber objects using your
own code. See
Session.getSubscribersForStream().
For the streamDestroyed
event dispatched by a Publisher object, the default
behavior is that the Publisher object is removed from the HTML DOM. The Publisher object
dispatches a destroyed
event when the element is removed from the HTML DOM.
If you call the preventDefault()
method in the event listener for the
streamDestroyed
event, the default behavior is prevented, and you can
retain the Publisher for reuse or clean it up using your own code.
To see whether an event has a default behavior, check the cancelable
property of
the event object.
Call the preventDefault()
method in the event listener function for the event.