Library
Module
Module type
Parameter
Class
Class type
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
)
class type 'message messageEvent = 'message EventSource.messageEvent
Class type to define a messageEvent
type 'a message = 'a messageEvent Js.t
Shortcut for a messageEvent
class type 'message broadcaster = object ... end
Interface of a BroadcastChannel
type 'a t = 'a broadcaster Js.t
Shortcut for a broadcaster
Returns true
if BroadcastChannel is supported by the client's browser, false otherwise.
val create : string -> 'message t
Creates a BroadcastChannel with a name. Raise 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
Creates a BroadcastChannel with a name. Raise 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
Closes the channel object, indicating it won't get any new messages, and allowing it to be, eventually, garbage collected.
val name : 'message t -> string
Returns a string
, the name of the channel.
val post : 'message t -> 'message -> unit
Sends the message, of the broadcaster type to each BroadcastChannel 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 Dom.Event.typ ->
('a t, 'a message) Dom.event_listener ->
bool Js.t ->
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.
val message : 'a t -> 'a message Dom.Event.typ
An event to be used with addEventListener
val lwt_js_message : ?use_capture:bool -> 'a t -> 'a messageEvent Js.t Lwt.t
An event to be used with Lwt_js_events