Back to Content

RTCDataChannelEvent

files/en-us/web/api/rtcdatachannelevent/index.md

latest1.6 KB
Original Source

{{APIRef("WebRTC")}}

The RTCDataChannelEvent interface represents an event related to a specific {{DOMxRef("RTCDataChannel")}}.

{{InheritanceDiagram}}

Constructor

  • {{DOMxRef("RTCDataChannelEvent.RTCDataChannelEvent", "RTCDataChannelEvent()")}}
    • : Creates a new RTCDataChannelEvent.

Instance properties

Also inherits properties from {{DOMxRef("Event")}}.

  • {{DOMxRef("RTCDataChannelEvent.channel", "channel")}} {{ReadOnlyInline}}
    • : Returns the {{domxref("RTCDataChannel")}} associated with the event.

Examples

In this example, the datachannel event handler is set up to save the data channel reference and set up handlers for the events which need to be monitored. The {{domxref("RTCDataChannelEvent.channel", "channel")}} property provides the {{domxref("RTCDataChannel")}} representing the connection to the other peer.

js
pc.ondatachannel = (event) => {
  inboundDataChannel = event.channel;
  inboundDataChannel.onmessage = handleIncomingMessage;
  inboundDataChannel.onopen = handleChannelOpen;
  inboundDataChannel.onclose = handleChannelClose;
};

See A simple RTCDataChannel sample for another, more complete, example of how to use data channels.

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • WebRTC
  • {{domxref("RTCDataChannel")}}
  • A simple RTCDataChannel sample
  • {{domxref("RTCPeerConnection")}} (the target interface for {{DOMxRef("RTCPeerConnection.datachannel_event", "datachannel")}} events)