Channel Messaging API - Web APIs 编辑
The Channel Messaging API allows two separate scripts running in different browsing contexts attached to the same document (e.g., two IFrames, or the main document and an IFrame, two documents via a SharedWorker
, or two workers) to communicate directly, passing messages between one another through two-way channels (or pipes) with a port at each end.
Note:
This feature is available in Web Workers.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.
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
MessagePort
s; an experimental solution to allow broadcasting of a message to multiple ports simultaneously.
Examples
- We have published a channel messaging basic demo on Github (run it live too), which shows a really simple single message transfer between a page and an embedded
<iframe>
. - You can also see a multimessaging demo (run this live), which shows a slightly more complex setup that can send multiple messages between main page and IFrame.
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'Channel messaging' in that specification. | Living Standard |
Browser compatibility
MessageChannel
BCD tables only load in the browser
MessagePort
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论