Giter Site home page Giter Site logo

xvw / jsoo_broadcastchannel Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 2.0 280 KB

Binding in Js_of_ocaml for the BroadcastChannel API

Home Page: https://github.com/xvw/jsoo_broadcastchannel

License: MIT License

Makefile 1.99% OCaml 92.45% HTML 5.57%
ocaml binding broadcast-channel javascript

jsoo_broadcastchannel's Introduction

Jsoo_broadcastchannel

Checkout the documentation

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

Jsoo_broadcastchannel is a binding for the BroadcastChannel Api.

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 another file with onmessage

(* 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

(* 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 convenient usage, without type annotation)

(* 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
    )

Special thanks

I would like to sincerely thank @drup for his advice on the implementation and the design of the API !

Sample

An example (see the example directory)

jsoo_broadcastchannel's People

Contributors

falconpilot avatar hhugo avatar xvw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

falconpilot hhugo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.