Page
Library
Module
Module type
Parameter
Class
Class type
Source
Broadcast_channel
Sourcejsoo_broadcastchannel
provides a wrapper around the Broadcast_channel API in JavaScript. The Broadcast_channel interface represents a named channel that any browsing context of a given origin can subscribe to. It allows communication between different documents (in different windows, tabs, frames or iframes) of the same origin. Messages are broadcasted via a message event fired at all Broadcast_channel objects listening to the channel.
Example of use :
Creating a channel an post message (on a first file) :
let channel = Broadcast_channel.create "my_first_channel"
let _ = Broadcast_channel.post channel (Js.string "Hello World")
Receiving message from the channel my_first_channel
on an another file with onmessage
function :
(* Retreive the channel *)
let channel : Js.string Js.t Broadcast_channel.t =
Broadcast_channel.create "my_first_channel"
(* You have to fix the type of the channel, you can also use [Broadcast_channel.create_with] *)
let _ =
Broadcast_channel.on
channel
(fun ev ->
(* Use the ev object *)
Js._true
)
Receiving message from the channel my_first_channel
on an another file with addEventListener
function :
(* Retreive the channel *)
let channel : Js.string Js.t Broadcast_channel.t =
Broadcast_channel.create "my_first_channel"
(* You have to fix the type of the channel, you can also use [Broadcast_channel.create_with] *)
let _ =
Broadcast_channel.addEventListener
channel
(Broadcast_channel.message channel)
(Dom.handler (fun ev -> ... Js._true))
Js._true
Or you can use Broadcast_channel.create_with
(for a more conveinent usage)
(* Retreive the channel *)
let (channel, message_event) =
Broadcast_channel.create_with
"my_first_channel"
(Js.string "a sample")
let _ =
Broadcast_channel.addEventListener
channel
message_event
(Dom.handler (fun ev -> ... Js._true))
Js._true
Receiving message from the channel my_first_channel
on an another file with Lwt_js_events
:
(* Retreive the channel *)
let channel : Js.string Js.t Broadcast_channel.t =
Broadcast_channel.create "my_first_channel"
let _ =
Lwt_js_events.async_loop
Broadcast_channel.lwt_js_message
channel
(fun ev _ ->
...
Lwt.return_unit
)
Exception if Broadcast_channel is not supported
Class type to define a messageEvent
Shortcut for a messageEvent
Interface of a Broadcast_channel
Shortcut for a broadcaster
Returns true
if Broadcast_channel is supported by the client's browser, false otherwise.
Creates a Broadcast_channel with a name. Raise Not_supported "Broadcast_channel"
if Broadcast_channel is not supported by the client's browser.
Creates a Broadcast_channel with a name. Raise Not_supported "Broadcast_channel"
if Broadcast_channel is not supported by the client's browser. The functions takes a "sample of a message" to fix the types of the broadcaster. The functions returns a couple of the Broadcast_channel and the Event
(to be used) in addEventListener
.
Closes the channel object, indicating it won't get any new messages, and allowing it to be, eventually, garbage collected.
Sends the message, of the broadcaster type to each Broadcast_channel object listening to the same channel.
Is an EventHandler
property that specifies the function to execute when a message event is fired on this object.
val addEventListener :
'a t ->
'a message Js_of_ocaml.Dom.Event.typ ->
('a t, 'a message) Js_of_ocaml.Dom.event_listener ->
bool Js_of_ocaml.Js.t ->
Js_of_ocaml.Dom.event_listener_id
Add an event listener. This function matches the addEventListener
DOM method, except that it returns an id for removing the listener.
An event to be used with addEventListener
val lwt_js_message :
?use_capture:bool ->
?passive:bool ->
'a t ->
'a messageEvent Js_of_ocaml.Js.t Lwt.t
An event to be used with Lwt_js_events