module BroadcastChannel:sig
..end
jsoo_broadcastchannel
provides a wrapper around the BroadcastChannel API
in JavaScript.
The BroadcastChannel 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 BroadcastChannel objects listening
to the channel.
Example of use :
Creating a channel an post message (on a first file) :
let channel = BroadcastChannel.create "my_first_channel"
let _ = BroadcastChannel.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 BroadcastChannel.t =
BroadcastChannel.create "my_first_channel"
(* You have to fix the type of the channel, you can also use [BroadcastChannel.create_with] *)
let _ =
BroadcastChannel.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 BroadcastChannel.t =
BroadcastChannel.create "my_first_channel"
(* You have to fix the type of the channel, you can also use [BroadcastChannel.create_with] *)
let _ =
BroadcastChannel.addEventListener
channel
(BroadcastChannel.message channel)
(Dom.handler (fun ev -> ... Js._true))
Js._true
Or you can use BroadcastChannel.create_with
(for a more conveinent usage)
(* Retreive the channel *)
let (channel, message_event) =
BroadcastChannel.create_with
"my_first_channel"
(Js.string "a sample")
let _ =
BroadcastChannel.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 BroadcastChannel.t =
BroadcastChannel.create "my_first_channel"
let _ =
Lwt_js_events.async_loop
BroadcastChannel.lwt_js_message
channel
(fun ev _ ->
...
Lwt.return_unit
)
exception Not_supported
class type['message]
messageEvent =['message]
EventSource.messageEvent
type'a
message ='a messageEvent Js.t
class type['message]
broadcaster =object
..end
type'a
t ='a broadcaster Js.t
val is_supported : unit -> bool
true
if BroadcastChannel is supported by the
client's browser, false otherwise.val create : string -> 'message t
Not_supported "BroadcastChannel"
if BroadcastChannel is not supported by the client's browser.val create_with : string ->
'a -> 'a t * 'a message Dom.Event.typ
Not_supported "BroadcastChannel"
if BroadcastChannel 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 BroadcastChannel and the Event
(to be used)
in addEventListener
.val close : 'message t -> unit
val name : 'message t -> string
string
, the name of the channel.val post : 'message t -> 'message -> unit
val on : 'message t ->
('message message -> bool Js.t) -> unit
EventHandler
property that specifies the function to execute
when a message event is fired on this object.val addEventListener : 'a t ->
'a message Dom.Event.typ ->
('a t, 'a message) Dom.event_listener ->
bool Js.t -> Dom.event_listener_id
addEventListener
DOM method, except that it returns an id for removing the listener.val message : 'a t -> 'a message Dom.Event.typ
addEventListener
val lwt_js_message : ?use_capture:bool ->
'a t -> 'a messageEvent Js.t Lwt.t
Lwt_js_events