Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Getting started with cross-domain messaging

From: HTML5 Power Workshop

Video: Getting started with cross-domain messaging

HTML5 has a communication API which allows web pages to talk to each other even if they've been downloaded from different web servers. Now, this is quite a powerful mechanism. You could create a client side mashup, where you have content being pulled in from lots of different service providers, such as the current weather or stock tick pricer. And you can do all that accumulation and aggregation client side, because you can now communicate with different web pages that have been downloaded, from different servers.

Getting started with cross-domain messaging

HTML5 has a communication API which allows web pages to talk to each other even if they've been downloaded from different web servers. Now, this is quite a powerful mechanism. You could create a client side mashup, where you have content being pulled in from lots of different service providers, such as the current weather or stock tick pricer. And you can do all that accumulation and aggregation client side, because you can now communicate with different web pages that have been downloaded, from different servers.

So I'm going to show in this lesson how the mechanism works. There are three files involved. The first file is main window the HTML, and as you can see I've downloaded it from a domain called MyDomain 1. I'm going to deposit some money into a bank account. I'll deposit a 100 pounds, and then I'll deposit 200 pounds and then withdraw 50 pounds. So, there's a global variable on this page that keeps track of the balance.

Now, I can also click this new Window button. Look what happens when I click this button a few times. It creates a few child windows on the right hand side. I've done some maths, to make them look in a nice position. Each new window has been downloaded from a different domain, and that's the key point here. The child windows come from MyDomain 2. The main window comes from MyDomain 1. So normally, before HTML5, you wouldn't be able to talk back and forth across these pages, because of security restraints, but you can do that now in HTML5.

So, to illustrate, when I click the Broadcast button, it will post the message to each child window, telling it the new balance. So, I click the button, broadcast and as you can see, each of the child windows has received a message with the new balance and displayed the new balance on the page here. So, there's a function called post message, which allows the main window to post a message to each child window to tell it to do something. You can also post messages in the reverse direction using the same approach. When I click the Levy Fee button, it posts a message back to the main window to say, take 10 pounds off the bank balance, it's like a bank charge. So, when I click the button here, it will post the message back to the main window and there's an important point here.

Whenever you post a message either from the main window to the child window or from the child window back to the main window, the receiver always has the opportunity to see who sent the message. And you can decide whether you want to accept the message or reject it based on whether you trust the domain or not. This is called the origin, and the origin includes the schemer, suggests HTTP and the domain name such as MyDomain 2. And potentially the port number as well, so the origin identifies who sent the message.

In this case, the sender was MyDomain 2, and you get the chance in the receiver to decide whether to accept the message or reject the message. Now, in my code, I accept the message. So, it will have reduced the balance by 10 pounds. And automatically, it rebroadcasts the new balance to each of the child windows. So, the child windows now show a balance of 240 as opposed to 250, if I do it again. And you'll see the balance go from 240 down to 230.

For this example, to work I have to set my files up, so that they came from different web servers. On the on machine, if you have several different web servers, then the example is quite straight forward. Copy main window onto one server, and copy child window onto another. On my machine, I just have one web server, which is Microsoft IIS. And I've copied both of these files into the same folder, which is inetpub\wwwroot. I'll just show you what that folder looks like on my machine. So you can see main window and child window, are actually on the same web publishing folder plus the style sheet, that makes all the buttons look pretty.

So, what I had to do is I had to setup some aliases, so that MyDomain 1 and MyDomain 2 pretend to be different servers. The way you do that on Windows is in a file called hosts, here it is. I've opened it in the edit the hosts and is in this location c:windows\systems32\drivers\etc, that's a place where you can set up aliases for your web server. So at the end of the file, I added these two statements. I setup two aliases, MyDomain 1 is an alias for localhost 127.0.0.1 is localhost.

So, MyDomain 1 is localhost effectively, and MyDomain 2 also is localhost. But as far as the application is concerned they are different domains. There's one more important point I want to show in the example. I'm going to change my main window to be localhost instead of MyDomain 1 localhost. Okay, and I will just close the other child windows, that I have opened up earlier just avoid any potential confusion.

So now, I've downloaded the main window from localhost as supposed to MyDomain 1. So, I'll just deposit the money 200 pounds, lets make it 20,000 pounds deposit. I created some child windows, now watch what happens when I click the Broadcast button, sending it from localhost to these child windows over here on MyDomain 2. So, click the Broadcast button notice this, I've actually got some logic in the child windows it checks the sender and if the origin is anything other than MyDomain 1, it rejects it. So, this illustrates the opportunity that you have, as the receiver of messages, to check the origin of the sender, and you can decide whether you trust it or not. You can decide whether you want to accept the message or reject the message. A very powerful mechanism that's completely safe and that allows child windows to talk to each other, even if they've come from different domains, in a secure manner.

Show transcript

This video is part of

Image for HTML5 Power Workshop
HTML5 Power Workshop

22 video lessons · 3885 viewers

Andy Olsen
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML5 Power Workshop.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.