Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

API Channel Messaging

Cette traduction est incomplète. Aidez à traduire cet article depuis l'anglais.

L'API Channel Messaging permet à deux scripts séparés s'exécutant dans des contextes de navigation attachés au même document (e.g., deux IFrames, ou le document principal et un IFrame, deux documents via un SharedWorker, ou deux workers) de communiquer directement, en se passant des messages de l'un à l'autre à travers des canaux bidirectionnels (ou pipes) pourvus d'un port à chaque extrémité.

Channel messaging concepts and usage

A message channel is created using the MessageChannel() constructor. Once created, the two ports of the channel can be accessed through the MessageChannel.port1 and MessageChannel.port2 properties (which both return MessagePort objects.) The app that created the channel uses port1, and the app at the other end of the port uses port2 — you send a message to port2, and transfer the port over to the other browsing context using window.postMessage along with two arguments (the message to send, and the object to transfer ownership of, in this case the port itself.)

When these transferable objects are transferred, they are 'neutered' on the previous context — the one they previously belonged to. For instance a port, when is sent, cannot be used anymore by the original context. Note that the only two objects that can currently be transferred are ArrayBuffer and MessagePort.

The other browsing context can listen for the message using MessagePort.onmessage, and grab the contents of the message using the event's data attribute. You could then respond by sending a message back to the original document using MessagePort.postMessage.

When you want to stop sending messages down the channel, you can invoke MessagePort.close to close the ports.

Find out more about how to use this API in Using channel messaging.

Channel messaging interfaces

MessageChannel
Creates a new message channel to send messages across.
MessagePort
Controls the ports on the message channel, allowing sending of messages from one port and listening out for them arriving at the other.
PortCollection
An array of MessagePorts; an experimental solution to allow broadcasting of a message to multiple ports simultaneously.

Examples

Specifications

Specification Status Comment
WHATWG HTML Living Standard
Channel messaging defined in section 9.5
Standard évolutif No difference from HTML5 Web Messaging.
HTML5 Web Messaging Candidat au statut de recommandation W3C version of the spec

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 4 Pas de support 10.0 10.6 5
PortCollection Pas de support Pas de support Pas de support Pas de support Pas de support
Feature Android Chrome Mobile Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 4.4 4 Pas de support Pas de support 10.0 11.5 5.1
PortCollection Pas de support Pas de support Pas de support Pas de support Pas de support Pas de support Pas de support

Compatibility notes

Channel messaging support for Firefox is close; MessagePort is already being used in shared workers. See bug 952139 for progress.

See also

Étiquettes et contributeurs liés au document

 Contributeurs à cette page : jean-pierre.gay
 Dernière mise à jour par : jean-pierre.gay,