Skip to content

Repository files navigation

react-peer

NPM version NPM downloads Build Status

Send state to peers as easy as a setState() 🎣👪.

Uses peerjs under the hood, requires a version of react/react-dom which contain hooks - at the moment this is 16.8.0-alpha.1.

Installation

npm install react-peer
yarn add react-peer

Usage

usePeerState(initialState?: TState, opts?: { brokerId?: string }): [TState, Function, string, Peer.DataConnection[]];

Behaves as your regular useState hook, but will eventually send data to any connected peers. Peers can connect to you using the brokerId that is eventually returned.

opts.brokerId is optionally used when you already have a broker id generated.

import { usePeerState } from 'react-peer';

const App = () => {
  const [state, setState, brokerId, connections] = usePeerState();

  setState({ message: 'hello' });
};

useRecievePeerState(peerBrokerId: string, opts?: { brokerId?: string }): [TState | undefined, boolean];

Will recieve peer state eventually from a peer identified using peerBrokerId.

opts.brokerId is optionally used when you already have a broker id generated.

import { useRecievePeerState } from 'react-peer';

const App = () => {
  const [state, isConnected] = useRecievePeerState('swjg3ls4bq000000');
};

About

Send data to someone else's browser as easy as setting state

Topics

Resources

Stars

194 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages