So I want to go into the message channel API and see how the same example, we can implement it with a message channel. So first we're going to create a message channel, as simple as that. And then I want to talk about these two things. We have the port one and port two. These are properties in the channel, and they're deeply connected. When I do onMessage on port one, it will receive messages from port two. So on port two, I can do postMessages and it will come to the port one onMessage. On port one, I can do postMessage, and it will arrive in the port two onMessage. So they are deeply connected and this is like the channel that is between them.
So this time we are still using postMessage, but there is something special going on in here. We are sending the second port in the postMessage payload, specifically in the argument that is called transferable objects, a message port is one of them. And this allows us to... This allows the embedded iframe to receive this message port. So we can see here the same embedded iframe, but now this event that ports zero, it's actually port two. So now we can do a postMessage on that, and it's going to come back to the onMessage on the first port. So I want to talk about a bit what went here. The line of communication was safe. We had that direct line from the port two to port one. Nobody saw anything in between. And it opens a lot of possibilities. So message channel is great, but it can still be a bit complex. There is a lot of boilerplate code that we will usually want to create, like handshake between the mainframe and the embedded iframe or mainframe and the webworker. This is much easier to implement than the naive approach, but still, it requires a bit of boilerplate. And we are here in the React Advanced Conference, and if we want to use refrec, it also requires a bit of wrapping. So I've created this library, it's called message-channel-shake. It's really in an alpha state, but I urge you to look in the source code to see a bit how the boilerplate there is implemented. Maybe it will be even better and you can use it out of the box, but it has all those things that we want, all the boilerplates, out of the box.
So it's handshake, bidirectional communication, promise support, iframe and web worker support, and React support. Let's see an example with React, and that's why we're here, and see how we can implement the same thing we saw before, but with message-channel-shake. So the initiator is the main page, and we first wrap our app with the message-channel-shake provider, a React provider, and then somewhere inside the component tree, we're gonna use the iframe-channel-wrapper, and the first properties are the same properties you could pass to a regular iframe, and then these are some special properties you need to pass to your message-channel-shake implementation. So first there's the channel ID, both the main page and the embedded iframe need to talk in the same channel, so it will be secure, and then we have message callbacks, so the main page, the initiator, can handle events from the embedded iframe or WebWalker. I want you to notice that the callback is async, so you can actually do async logic and return a response, and it will go back to the embedded iframe. Let's see how it looks in the embedded iframe, so in the embedded iframe, we have a receiver provider, which has the same set of properties that's needed like before, and somewhere inside the component for the receiver, you can use a hook that is called useportmessenger, and in the portmessenger, we can just send messages, and that is actually a promise. In the response, we can get a payload back from the main page message handler, and we can do whatever we want, so a bit of a summary, it was quick. What did we see? We've seen how to communicate naively between iframes, we've seen how to use message channel, we've seen how message channel shake can help leveraging message channel and reduce a bit of the boilerplate. If you want to further enhance some communication knowledge between iframes, web workers, tabs, I urge you to read about these technologies. There is broadcast channel, it's also a web API, there is system, it's a library to allow cross-region communication in broadcast channel, and there are transferable objects that are more than the other message port.
Comments