-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBroadcastChannel.js
More file actions
44 lines (33 loc) · 982 Bytes
/
BroadcastChannel.js
File metadata and controls
44 lines (33 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* The Broadcast Channel API allows basic communication between browsing
* contexts (that is, windows, tabs, frames, or iframes) and workers on the same origin.
*/
bc = new BroadcastChannel("carrito_amazon");
function addpProduct(product){
// aqui llamada al backend
bc.postMessage({
action: "add",
product: {
id: 1,
name: "Bambas de correr",
price: 100,
quantity: 1,
},
});
}
// otra pestaña - Suscripción
bc.onmessage = (event) => {
if(event.data.action === "add") {
// añadir a la UI el producto de la pestaña
console.log(event.data.product);
}
}
//-----------------------------------------------------------------------
// Canal de mensajes
bc = new BroadcastChannel('canal_mensajes');
// Escuchar mensajes
bc.onmessage = (event) => {
console.log('Mensaje recibido:', event.data);
};
// Enviar un mensaje
bc.postMessage('Hola a todos');