We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

Talking Tabs - Communicating Across Browsing Contexts

00:00

Formale Metadaten

Titel
Talking Tabs - Communicating Across Browsing Contexts
Untertitel
The talk covers different ways on how to communicate across browsing contexts/tab and explains the use-case for each of them.
Serientitel
Anzahl der Teile
637
Autor
Mitwirkende
Lizenz
CC-Namensnennung 2.0 Belgien:
Sie dürfen das Werk bzw. den Inhalt zu jedem legalen Zweck nutzen, verändern und in unveränderter oder veränderter Form vervielfältigen, verbreiten und öffentlich zugänglich machen, sofern Sie den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen.
Identifikatoren
Herausgeber
Erscheinungsjahr
Sprache
Produktionsjahr2021

Inhaltliche Metadaten

Fachgebiet
Genre
Abstract
The presentation will cover different ways to communicate across browsing contexts. The main 2 approaches are - 1. Shared memory - Localstorage, Cookies, SessionStorage, IndexedDB, SharedArrayBuffers & Atomics 2. Messaging- Postmessage, BroadcastChannel, MessageChannel The presentation will show a demo of usage for each of the types and explain the use-case, advantages and disadvantages for them.
179
Vorschaubild
20:09
245
253
Vorschaubild
30:06
294
350
Vorschaubild
59:28
370
419
491
588
Vorschaubild
30:18
Überlagerung <Mathematik>Computeranimation
BestimmtheitsmaßMereologieSoftware EngineeringSystemaufrufOpen SourceKontextbezogenes SystemTelekommunikationBildschirmfensterXMLUMLComputeranimation
SoftwareOpen SourceSoftware EngineeringRelativitätstheorieOpen SourceMereologieRISCAdditionDelisches Problemt-TestGesetz <Physik>XML
TelekommunikationHalbleiterspeicherCookie <Internet>TermObjekt <Kategorie>Kontextbezogenes SystemProgrammierumgebungZeitbereichNummernsystemProtokoll <Datenverarbeitungssystem>AppletSkriptspracheAbstraktionsebeneCodeDatensichtgerätUmwandlungsenthalpieSchaltnetzMereologieNummernsystemKontextbezogenes SystemProgrammierumgebungMessage-PassingDifferenteProzess <Informatik>Quick-SortViewerMatchingKonstruktor <Informatik>BildverstehenAbstraktionsebeneGemeinsamer SpeicherProtokoll <Datenverarbeitungssystem>CASE <Informatik>WhiteboardCodeMinkowski-MetrikHalbleiterspeicherDomain <Netzwerk>ATMTermBrowserWeb-SeiteBildschirmfensterBroadcastingverfahrenURLXML
Gemeinsamer SpeicherMultiplikationsoperatorSpeicher <Informatik>BeobachtungsstudieKontextbezogenes SystemKategorie <Mathematik>Stellenring
BrowserSchnittmengeMatchingAggregatzustandSpeicher <Informatik>StellenringMathematikPhysikalisches SystemBildschirmfensterNummernsystemDezimalzahlComputerarchitekturKontextbezogenes SystemQuick-SortWhiteboardSchlüsselverwaltungBootenJSONXML
Architektur <Informatik>TelekommunikationDemo <Programm>Speicher <Informatik>MathematikKontextbezogenes SystemMailing-ListeBitrateComputeranimation
Quick-SortMultifunktionBenutzerbeteiligungQuaderBenutzeroberflächeBrowserWeb-SeiteGemeinsamer SpeicherSpeicher <Informatik>Kontextbezogenes SystemComputeranimation
EntscheidungsmodellToken-RingNotepad-ComputerZeichenketteHasard <Digitaltechnik>ComputersicherheitKonditionszahlMechanismus-Design-TheorieDifferenteBrowserATMGemeinsamer SpeicherKontextbezogenes SystemSpeicher <Informatik>RechenschieberQuick-SortCASE <Informatik>Demo <Programm>SkriptspracheMultiplikationsoperatorEinfügungsdämpfungZeichenketteMechanismus-Design-TheorieBrowserInverser LimesProdukt <Mathematik>FunktionalWeb SiteStellenringComputersicherheitInformationHasard <Digitaltechnik>DatentypApp <Programm>Nichtlinearer OperatorSynchronisierungEinflussgrößeSerielle SchnittstelleKonditionszahlCookie <Internet>Formation <Mathematik>Exogene VariableQuelle <Physik>Brennen <Datenverarbeitung>HackerWellenpaketBestimmtheitsmaßAdditionMereologieAggregatzustandComputeranimationJSONXMLUML
Cookie <Internet>ZeichenketteBrowserExogene VariableCookie <Internet>ComputerarchitekturDateiformatZeichenketteGarbentheorieServerE-MailBrowserExogene VariableEindeutigkeitTypentheorieMereologieBenutzerbeteiligungBildschirmmaskeQuick-SortTotal <Mathematik>JSONXML
TelekommunikationArchitektur <Informatik>Cookie <Internet>Kontextbezogenes SystemFunktionalGruppenoperationBildschirmfensterAggregatzustandCASE <Informatik>MultiplikationsoperatorDemo <Programm>SynchronisierungMultiplikationSchnittmengeBenutzerbeteiligungSchlüsselverwaltungComputeranimation
Cookie <Internet>FunktionalQuellcodeSpeicher <Informatik>Cookie <Internet>BitQuick-SortMathematikSchnittmengeTablet PCSpieltheorieKonditionszahlWort <Informatik>DifferenteComputeranimation
AuthentifikationEINKAUF <Programm>ObjektverfolgungEntscheidungsmodellNotepad-ComputerCookie <Internet>Gemeinsamer SpeicherKartesische KoordinatenToken-RingOverhead <Kommunikationstechnik>CASE <Informatik>Cookie <Internet>Komplex <Algebra>AuthentifikationTypentheorieQuick-SortHalbleiterspeicherMailing-ListeJSONXML
BroadcastingverfahrenMechanismus-Design-TheorieBrowserRahmenproblemBroadcastingverfahrenMessage-PassingTelekommunikationQuick-SortAdditionInformationSoftwaretestKontextbezogenes SystemBildschirmfensterPunktProzess <Informatik>Gebäude <Mathematik>Figurierte ZahlPhysikalisches SystemCross-site scriptingBrowserComputerarchitekturDienst <Informatik>Wort <Informatik>BenutzerbeteiligungJSONXMLUML
TelekommunikationArchitektur <Informatik>Konvexe HülleComputervirusBroadcastingverfahrenMessage-PassingFigurierte ZahlSoftwaretestEreignishorizontDemo <Programm>MereologieGebäude <Mathematik>Quick-SortKontextbezogenes SystemBrowserComputeranimation
BroadcastingverfahrenGruppenoperationWeb logDigitale PhotographieNotepad-ComputerBroadcastingverfahrenPhysikalisches SystemGruppenoperationWeb SiteCASE <Informatik>MultiplikationSynchronisierungInstantiierungEinfache GenauigkeitSkriptspracheArithmetisches MittelOrdnung <Mathematik>ComputeranimationXML
ThreadKontextbezogenes SystemMessage-PassingTypentheorieDienst <Informatik>BenutzerbeteiligungSkriptspracheTypentheorieThreadWeb-ApplikationMessage-PassingArithmetische FolgePunktCodeQuick-SortDienst <Informatik>Inhalt <Mathematik>GruppenoperationArithmetisches MittelNichtlinearer OperatorEin-AusgabeFunktion <Mathematik>VerkehrsinformationAusnahmebehandlungZustandsdichteWasserdampftafelForcingComputeranimation
SkriptspracheProzess <Informatik>ThreadTelekommunikationArchitektur <Informatik>SkriptspracheQuick-SortBenutzerbeteiligungKontextbezogenes SystemPunktSpiraleMessage-PassingElektronische PublikationProzess <Informatik>Demo <Programm>ThreadDiagrammXMLUMLComputeranimation
Demo <Programm>TelekommunikationBildschirmmaskeExogene VariableQuick-SortArithmetisches MittelThreadComputeranimation
VisualisierungTaskRechnernetzRechenbuchBefehlsprozessorNotepad-ComputerBildverarbeitungNichtlinearer OperatorBildverarbeitungSoftwaret-TestCASE <Informatik>Speicher <Informatik>StellenringBildgebendes VerfahrenVisualisierungAutomatische IndexierungXMLUML
BildschirmfensterDifferenteSkriptspracheJSONXMLUML
SkriptspracheEindeutigkeitProzess <Informatik>Message-PassingArchitektur <Informatik>TelekommunikationDemo <Programm>PunktSkriptspracheWhiteboardQuick-SortDifferenteTelekommunikationDrahtloses lokales NetzKontextbezogenes SystemGesetz <Physik>Objekt <Kategorie>ARM <Computerarchitektur>MereologieComputerarchitekturThreadMessage-PassingKlasse <Mathematik>DiagrammComputeranimation
Gemeinsamer SpeicherWurm <Informatik>ComputervirusDemo <Programm>ParametersystemBroadcastingverfahrenQuick-SortBasis <Mathematik>Elektronische PublikationProfil <Aerodynamik>StellenringComputerspielForcingRahmenproblem
Funktion <Mathematik>EreignishorizontMessage-PassingComputervirusIndexberechnungMessage-PassingWhiteboardEinfach zusammenhängender RaumQuick-SortGesetz <Physik>MathematikStellenringCASE <Informatik>Digitale PhotographieRechter WinkelThreadBroadcastingverfahrenRahmenproblemEreignishorizont
SocketNotepad-ComputerBrowserLipschitz-StetigkeitEinfach zusammenhängender RaumWort <Informatik>Gemeinsamer SpeicherSocketQuick-SortCASE <Informatik>AggregatzustandBenutzerbeteiligungMobiles InternetBrowserTypentheorieSpeicher <Informatik>Puffer <Netzplantechnik>DatenstrukturZahlenbereichSynchronisierungKugelkappeStellenringAssoziativgesetzFacebookAutomatische IndexierungTeilbarkeitJSONXML
FacebookJSONXMLUMLComputeranimation
Element <Gruppentheorie>MereologieComputeranimation
StellenringUmwandlungsenthalpieKonditionszahlSpeicher <Informatik>ForcingMathematische LogikCASE <Informatik>Cookie <Internet>Quick-SortProgrammiergerätComputervirusSchnittmengeRechter WinkelBroadcastingverfahrenTelekommunikationBesprechung/Interview
HardwareSkriptspracheAppletComputeranimation
Element <Gruppentheorie>Computeranimation
Transkript: Englisch(automatisch erzeugt)
Hello everyone, hope everyone is doing good and are safe. Today I will be presenting a talk titled Talking Tabs, Communicating across browsing context. By the end of this talk, we can expect to understand the different ways of communicating across tabs or windows. This is the main agenda of this topic.
Before we move forward, I will quickly introduce myself. I am Akshat and I am based out of India. I am working as a software engineer with Uber as a part of the RISC team. I have been associated with open source for the last 5 years now. It all started with me making contributions to orgs during my college days like Mozilla, Firefox etc.
Since then, my love for the community has been ever growing. In my free time, I love to read about technology and related topics. Without any further delay, let us move into the main agenda.
As the topic reads, Communicating across browsing context. Let us see the primary modes of communicating here. The first one is the shared memory approach. In this case, we have a shared memory space where different browsing contexts can read or write data from. Enabling in a way how to communicate right.
The second way is to communicate via messaging. Two different messaging styles like post message style messaging, broadcast message etc. We will be discussing some of them in details in the latter half of the session. Before we go into the specifics, here are some related terms used across this talk.
Firstly, browsing context. By browsing context, we actually mean the environment in which a browser displays a document. In modern browser, it usually is a tab but it can definitely be a window or even parts of a page like an iframe. Next is origin.
Origin is defined as a combination of the scheme which is the protocol, host which is the domain and the port of the URL used to identify a URL. When we say things to be of same origin, what we mean in that case is that the scheme, host and the port all of them match for a URL.
To give a very simple example. For example, one of the URL is http example.com slash app1 and second is http example.com slash app2. Here the scheme is http, the domain is example.com and the port is 80.
All three of them matches for these two URLs and we can hence say that they are of same origin. Lastly, explain the execution context. It is an abstract concept of an environment where the JavaScript code is actually evaluated and executed.
Moving on, without wasting any time, let us move on to the shared memory approach. The first one in this category is the local storage. Most of us might be familiar with it and might have used local storage sometime, somewhere.
I mean the set item method and the get item method, so we must be familiar with that. Now, let us look at it and see how we can communicate via this method across browsing context. Firstly, the features. Local storage is a key value store and the data is shared between all tabs and windows from the same origin.
Again, by same origin, we mean that the scheme, the hostname and the port of the URL should match. Lastly, the data does not expire even if we restart the browser or do an OS reboot. In a sense, it is persisted.
Let us now see the architecture. In this figure, we can see that one browsing context sets a value in the local storage via the set item method. On the other hand, the other browsing contexts are listening for any change in the storage. As soon as the value is updated in the storage, the other tab receives it in the listener callback and updates can be made based on the newer value.
Let us quickly see a demo of it. Here in this web page, we can see there are 5 counter UIs with an increment button.
The first one at the top corresponds to the button and the counter in the main document. 4 of the other boxes are actually iframes loaded onto this web page. We will click on the increment in the document. We can see that all the corresponding counters are immediately reflecting the updated value.
When we do it from the iframe, we can see the same. Let us try to open it in a new tab and then see. Let us increment from this tab and see it here. We can see that we can communicate across the browsing context via a shared memory that is local storage.
Let us go back to the slides. We saw a demo of it. Now, let us look at some use cases. One of them is storing shopping cart items, user preferences like themes and sometimes auth tokens as well.
Although, it is not safe to do that. Here in this case, we added listener on the storage which enables us to sync the information across browsing context.
Some gotchas to look out for. It is prone to security hazards. For example, if a site is vulnerable to accesses attacks, local storage is not safe in that way since any script can access local storage. Also, people many times tend to clear cache and that might lead to loss of data.
In a way, we can say that it has very limited data production measures. Next is it is prone to race condition as there is no locking mechanism on data. Next is that it can only store string data. This makes it pretty useless for storing data that is even slightly more complex than a string.
I am sure we can definitely serialize everything including data types into string and then store it. It is sort of a hack and also adds time for that serialization step. Next is that it is synchronous and that means that each of the operation in local storage happens one at a time.
For a complex application, it will definitely slow down our app's runtime. The last one is that it limits the size of data that we can store. It is around 5MB across all major browsers. This is fairly a low limit for people building apps.
There are data intensive and nowadays people also build offline functionalities. So that might need even more space. Next one is cookies. Let us quickly have a look at it as well. Cookies are small strings of data that are stored directly in the browser. The unique part is that it can be set by web servers as well using a response type set cookie http header.
The servers can set the set cookie http header and via that method set the cookie value as well. The value of the cookie consists of in the format name equal to value pairs delimited by a semicolon.
Each of that section of name equal to value pair is actually a separate individual cookie. We will have a look at the architecture now. Here we can actually see how the cookie looks like.
In this case there are two cookies. One with the key name underscore GA and other with the key name counter. Here one of the browsing contexts sets the cookie via the document dot cookie api. The other browsing context have a function called after every interval through the set interval web api.
To sync the cookie data cross browsing context. What it does is basically it calls the function multiple times to check whether the cookie has been updated. If yes then it can take appropriate action. Let us look at a demo at it as well.
Here the UI is pretty similar to the local storage just that the functionality inbuilt is using cookies rather than local storage. We will again click on the increment and we can see that again it is working and reflecting the values in both the ways.
Also you might have noticed that it is a bit slower compared to local storage. The reason is because we are using the set interval which sort of polls for any data change and that is the reason. It looks a bit laggy.
Moving on. Regarding the use cases. It is commonly used for authentication for storing tokens. Some of the other use cases are storing user preferences like themes or sometimes even cart items. Gotchas to look out for. It can store only 4kb of data which is comparingly very very less based on the increasing complexity of applications day by day.
Another overhead it comes is that we need to ask explicitly permission from the users to use cookies to comply with the GDPR. We had looked at the two types of shared memory approach.
Now we will be looking at the messaging approach. Firstly we will discuss about the broadcast channel method. By the word itself means that we are broadcasting some information right.
The broadcast channel API is a simple API that makes communicating between browsers context easier. I mean that is communicating between windows, tabs, iframes, web workers and service workers. Messages which are posted to a given channel are delivered to all listeners of that channel.
It is applicable for communication on same origin. This is one important point that for broadcast channel it is applicable for communication only in the same origin. Compared to a post message style communication where it can be done cross site as well. So in this we just broadcast a message to the channel and then it is
the browser's job to send it to the other browsing context listening to that channel. Looking at the architecture here. Here we can look at the figures. The broadcast channel named test is created and a hello message is broadcasted through the channel. The message can then be received in all the other tabs via the on message event listener.
This is how it works. Here is a demo of it. So here we can see again there are four iframes and one button as a part of this document. So we click on the say hi to all button and we can see that hi is being sort of broadcasted to all of the other sort of iframes that is browsing context.
And the vice versa is also true. And we can actually open a new tab and also see the same. So we say hello to all from this tab and we can see it getting reflected in this tab as well. So this is how the broadcast channel method works.
Moving on. So regarding the use cases. So I mean there are multiple use cases of it. So one of two few of them are for example we need to sync UI across tabs based on some user action. So basically if a user takes action in one of the tab and we need to update it across all tabs.
So this is one approach or one use case. And another example might be for example if a user logs out from a single tab and for example multiple instances of that website is opened. So we actually want the user to be logged on from all of them. So again broadcast channel can be used in this case.
So workers. So now let us come to the workers now. So web workers are I mean simple means to actually for web content to run scripts in the background thread. So it's usually done to offload some heavy lifting to the background thread without interfering the main thread that is the UI thread.
And it can perform sort of input output operations as well as sort of use XML HTTP request as well as sort of use the fetch API. And once created a worker can send message to the JavaScript code that I mean that created it. So for example it can sort of send messages to the JavaScript code that actually spawned the worker itself.
And one more point here is that the data is copied rather than being shared. So that means the reference of the data is separate in the main thread and the worker thread. So there are mainly three types of workers.
So dedicated workers, shared workers and service workers. So we will have a look at the first two in details. And regarding service workers. So these are mainly used to do background actions for example like push messaging and etc. And nowadays it is commonly used in the PWAs that is the progressive web apps as well.
So let us talk about dedicated workers. So it is sort of the most basic kind of web worker and its access is limited to only the script which is spawning it. So as I said it is accessible by the script which actually spawned the worker. It runs as a separate thread within the same process as the main script.
So this is one important point. And as well as one document can have multiple dedicated workers. So for example one worker is dedicated to one document but a single document can have multiple dedicated workers. So in this diagram we spawn a worker by passing in the worker.js file path.
Which contains the sort of the on message handler as well. And the sort of browsing context sends the message to the worker. And the worker can respond back to the script file which actually spawned it.
Let us look at the demo of it. So here we have the worker kind and we have a button called say hi to worker. So as soon as we click on this button we will get a response from the worker back. So the worker responds with hello from worker. So this is sort of a communication from the main thread to the worker thread.
Moving on. So regarding the use cases of it. So as I said it is used in heavy operations for example image processing, CPU intensive calculations, network intensive tasks, rendering optimizations, animations and visualizations.
So these are some of the use cases of dedicated workers. The gotchas here are that it does not have access to local storage. So sometimes we might want to sort of adapt to some hybrid solutions like we want to use local storage along with dedicated workers. But with it, it will not be possible with it.
And but it still has I mean it can access index.db so that is one thing. So lastly we will be talking about shared workers. So a shared worker is I mean compared to the dedicated worker it can be accessed by multiple scripts.
Even if they are being accessed I mean across different windows, iframes or even workers. So the difference mainly as I said is the ability to be accessed with I mean with different browsing context. So creating a shared worker is very similar to how we create a dedicated worker. But instead of the straight forward communication between the main thread and the worker thread.
We essentially have to communicate via a port object. I mean that is I mean an explicit port has to be opened to communicate to be opened so that multiple scripts can use it to communicate via the same sort of channel that is created. And also the point to be noted that I mean dedicated workers do this port creation and communication via this port implicitly.
And we do not have to sort of mention that. So looking at the architecture diagram. So similar to a dedicated worker we can spawn a shared worker using its class and passing in the path to the worker script.
And the document sort of communicates to the port that is there. So we can see shared worker dot port dot post message. We can post a message called hi. And again it can the shared worker it can respond back to the same port as hello here right.
So let us look at the demo. So here we have four iframes and one main document sort of here. So here we have a button for say hi to worker. So it will sort of say hi to the workers which is in the four iframes.
Say hello to all is kind of a broadcast we are seeing here. And say bonjour to alternate iframes so it will do it in an alternate basis. And we will also have a look at the worker file for this. So we click on say hi to worker. So we can see that basically it is saying hi to it is responding back hi from worker.
So we are saying hi and it is responding hi from worker. We say say hello to all and it is sort of a broadcast that we can see here. And next is we say say bonjour to alternate iframes. And we can see that this is being sort of printed in the alternate iframes here.
So let us quickly look at the js file as well for this. So yeah now we can see the worker js file right here. So here we can see different cases that we saw in the UI.
So in the first case when the event data was high that high was received from the main thread. We saw that it posted back a message hi from worker. In case of a hello it sort of looped through each of the port connection whichever ports were connected. And it replied back similar to a broadcast. And in case when bonjour was sort of sent through the data.
It sort of did a simple math and sort of sent it to the alternate frames. This is how it works. So moving back. So I think regards to the use cases of it. So it's generally useful whenever you want.
So we have use case to share web socket connections. Sort of maintain some kind of centralized state. Limiting active tabs. So this is one interesting use case where we can actually sort of have a sort of number of cap on the number of active tabs. That is useful. And coordinating among different tabs. For example yeah we can sync sort of UI states and other states across tabs.
And regarding the gotcha. So yeah it's not supported in Safari and mobile browsers. It does not have access to local storage. It can access IndexedDB and it does not cannot access shared array buffer. Which is again another type of data structure I would say.
So yeah this is what I wanted to cover in this talk. I hope it was pretty useful for each one of us here. And maybe you will take back some learnings from this talk. So and you can connect with me on GitHub, LinkedIn and Facebook.
And yeah now I am open for any questions now. Yeah thank you.
Hello everyone I mean feel free to ask any questions if you have any regarding any part of it.
I'll be happy to answer.
Yeah hi. Hi Ludovic. Hello. Thanks a lot for your talk. It was very interesting. Thank you.
Don't hesitate people to ask questions.
Yeah thanks for asking that question. So I mean to answer that question for you.
I mean recommended. So there is no strict recommended way for communicating. So it all depends on use to use I mean use by use cases. So I mean if you're looking for sort of a broadcast kind of a sort of logic. So in that case you might want to go with a specific set. But yeah it all depends on a specific user. Thanks for asking that question.
We still have five minutes. If you want to ask questions. Four minutes now. Don't hesitate guys.
Do you have some recommendation? Or something to add?
Yeah programmer I saw your question. So what what I meant to answer is basically it all depends on our use case. So we can we can use local storage cookies and whatnot right.
But it all depends on the use case that we're trying to solve for. And each of them has their own pros and cons. So for most of the use cases I think for simpler. Yeah programmer I saw your question. So whatever what I meant to answer is basically it.