package jsoo_broadcastchannel

  1. Overview
  2. Docs

Module Broadcast_channelSource

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

Sourceexception Not_supported

Exception if Broadcast_channel is not supported

Class type to define a messageEvent

Shortcut for a messageEvent

Sourceclass type 'message broadcaster = object ... end

Interface of a Broadcast_channel

Shortcut for a broadcaster

Common functions

Sourceval is_supported : unit -> bool

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

Sourceval 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.

Sourceval 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.

Sourceval close : 'message t -> unit

Closes the channel object, indicating it won't get any new messages, and allowing it to be, eventually, garbage collected.

Sourceval name : 'message t -> string

Returns a string, the name of the channel.

Sourceval post : 'message t -> 'message -> unit

Sends the message, of the broadcaster type to each Broadcast_channel object listening to the same channel.

Sourceval 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.

An event to be used with addEventListener

Sourceval 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

OCaml

Innovation. Community. Security.