Latest HTML Broadcastchannel API referrals

Source: Internet
Author: User

HTML Broadcastchannel API

Only Firefox38 in the current browser can support the Broadcastchannel API (at the point in time of writing this article), and Firefox38 officially announced that it will not release the official version until May 2015. This new set of APIs will open up a new world of possibilities, solving the many limitations that we have inherited from the PostMessage API.

Broadcastchannel API as part of the WHATWG living HTML standard can be viewed here for more information.

What is the Broadcastchannel API?

The Broadcastchannel API allows for interaction with all windows, IFRAMEs, and so on, under the same original domain and user agent. That is, if a user opens two tab windows for the same site, two windows will be notified at the same time if the content of the site has changed.

Or an unknown sensation? Take Facebook as an example, if you have now opened a window of Facebook, but you are not logged in at this time, you open another window to log in, then you can notify other windows/ tabs to tell them that a user has logged in and asked them to update their pages accordingly.

Essentially, the Broadcastchannel API allows us to create an application that is self-aware of state changes without the use of sockets and timers, which is particularly effective for a publish/subscribe form of system.

Broadcastchannel API Real-time to create a new Broadcastchannel

It's a breeze to create a new Broadcastchannel API. All you need to do is pass the channel name as a parameter to Broadcastchannel's constructor and save its reference to a variable.

New Broadcastchannel (' MyChannel ');

Send a message notification

Sending a message is also a very simple thing, you just have to refer to the variable that is endowed with the Broadcastchannel instance (in this case the cast variable above) and then call its PostMessage method.

If you are familiar with other publish/subscribe-based systems, it might be more reasonable to call the PostMessage member method an event emitter.

The nice thing about the PostMessage method is that you can use it to send anything. You can send an object, a string, whatever you please. As long as subscribers can be aware of what events you are sending, just enjoy it.

MYOBJ = {somekey: ' Some value ', Anotherkey: ' Another value '};cast.postmessage (MYOBJ);

Unlike some smaller meat-level publish/Subscribe systems, "Themes" are not supported by native implementations. It means that you don't have a channel to broadcast the "subject" to all the listeners who are listening.

However, by writing some creative code you can still imitate this implementation, you can use the object to the "subject" as a key object, the message content as another key "data" to send.

Message monitoring

The more popular term for "receivers" may be called "subscribers". A receiver will listen to the event that is emitted by the Broadcastchannel channel reference variable that is defined by a special channel as we have previously.

function (e) {     // This should print out the contents of the object we sent above}
Close connection

Assume that you are very concerned about the performance of JavaScript and that you care about resource consumption (especially in the case of using a phone). Fortunately, there is an inline method in the Broadcastchannel to let you close these connections.

// Close our connection and let the garbage collection free up the memory that is used
Browser support

As mentioned earlier, only Firefox version 38 currently supports the Broadcastchannel API. Assuming that other browsers will be very likely to follow, after all, this set of APIs is very useful. Then for us to write the common application and game application, we will be a duck in the way (the advent of tight browser support)

It is important to note that from a user's point of view you (currently) are basically not as relevant as the technical support. However, there is a browser plugin to let you use the Broadcastchannel API now, however, there is no browser to really implement the support of this feature, we only have to be waiting for.

Original: http://ilikekillnerds.com/2015/03/all-about-the-html-broadcastchannel-api/

Heaven Zhuhai Branch Rudder
Public Number: Techgogogo
Weibo: Http://weibo.com/techgogogo
Csdn:http://blog.csdn.net/zhubaitian

Latest HTML Broadcastchannel API referrals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.