GithubHelp home page GithubHelp logo

jhony-v / react-broadcast-channel Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 392 KB

๐Ÿ”Œ๐Ÿ“จ Implementation of BroadcastChannel API in react to emit and subscribe any messages to a particular channel.

Home Page: https://www.npmjs.com/package/react-web-broadcast-channel

License: MIT License

JavaScript 16.89% Shell 1.22% HTML 3.34% TypeScript 78.54%
react library emit-events broadcastchannel-api

react-broadcast-channel's Introduction

React broadcast channel

last commit version size licence code

Implementation of BroadcastChannel API in react to emit and subscribe any messages to a particular channel.

DEMO

Installation

To install, you can use npm or yarn:

$ npm install react-web-broadcast-channel
$ yarn add react-web-broadcast-channel

Examples

If you open other tab in your browser, you cant see how the message sent is updated in all tabs.

import React from 'react';
import { useBroadcastChannelEmitter } from "react-web-broadcast-channel"


const TEST_CHANNEL = "TEST_CHANNEL";

export default function App() {
  const { emit, data } = useBroadcastChannelEmitter(TEST_CHANNEL);

  const sendMessage = () => {
    emit(prompt())
  }


  return(
    <div>
      <button onClick={sendMessage}>emit event</button>
      <h1>{data}</h1>
    </div>
  )
}

Emiting messages to channel

import { useEffect } from "react";
import { 
  BroadcastSubscriber, 
  BroadcastEmiter, 
  useBroadcastChannelEmitter 
} from "react-web-broadcast-channel";

function EmitMessageFromA() {
    const { emit } = useBroadcastChannelEmitter(TEST_CHANNEL);
    
    const sendMessage = () => {
      emit(prompt())
    }

    return <button onClick={sendMessage}>send message from a</button>
}

function EmitMessageFromB() {
    return(
     <BroadcastEmiter channel={TEST_CHANNEL}>
       {(emit) => {
         return <button onClick={() => emit(prompt()) }>
           send message from b
         </button>    
       }}
     </BroadcastEmiter>
    )
}

Suscribe a channel

import { BroadcastSubscriber, useBroadcastChannelSubscribe } from "react-web-broadcast-channel";

function SubscriberA() {
  const { data } = useBroadcastChannelSubscribe(TEST_CHANNEL);
  useEffect(() => {
    console.log(data)
  },[data])

  return <div>Subscribe from A</div>
}

function SubscriberB() {
  return(
   <BroadcastSubscriber channel={TEST_CHANNEL}>
    {(data) => (
      <div>{data}<div>
    )}
   </BroadcastSubscriber>
  ) 
}

react-broadcast-channel's People

Contributors

jhony-v avatar

Watchers

 avatar

Forkers

atze187

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.