package jsoo_broadcastchannel

  1. Overview
  2. Docs

jsoo_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
    )

Exceptions and types

exception Not_supported

Exception if Broadcast_channel is not supported

class type 'message messageEvent = 'message Js_of_ocaml.EventSource.messageEvent

Class type to define a messageEvent

type 'a message = 'a messageEvent Js_of_ocaml.Js.t

Shortcut for a messageEvent

class type 'message broadcaster = object ... end

Interface of a Broadcast_channel

Shortcut for a broadcaster

Common functions

val is_supported : unit -> bool

Returns true if Broadcast_channel is supported by the client's browser, false otherwise.

val create : string -> 'message t

Creates a Broadcast_channel with a name. Raise Not_supported "Broadcast_channel" if Broadcast_channel is not supported by the client's browser.

val create_with : string -> 'a -> 'a t * 'a message Js_of_ocaml.Dom.Event.typ

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.

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 Broadcast_channel object listening to the same channel.

val on : 'message t -> ('message message -> bool Js_of_ocaml.Js.t) -> unit

Is an EventHandler property that specifies the function to execute when a message event is fired on this object.

Event support

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 Js_of_ocaml.Dom.Event.typ

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