Socket
Socket
Sign inDemoInstall

@thebigsasha/react-peerjs-hooks

Package Overview
Dependencies
16
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @thebigsasha/react-peerjs-hooks

Simple implementation of hooks for using PeerJS in a React App, compatible ssr and pre-compilers like NextJS and Gatsby


Version published
Weekly downloads
2
decreased by-60%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

PeerJS React Hooks

Use PeerJS in React with an interface somewhat similar to Appolo Client and the usual useState hooks.

Hosting a P2P Session:

Call the useHostPeerSession hook with the type of the state you want to share. The hook returns an array with the following values:

  1. The state of the partner
  2. The state of the host
  3. A function to set the state of the host
  4. A boolean indicating if the host is connected to the partner
  5. The ID of the host (which you send to someone who wants to join)
const [partnerState, myState, setMyState, isConnected, myID] =
  useHostPeerSession<StateInterface>({
    message: "Hi I'm hosting",
    color: '#00e5ff',
  });

Joining a P2P Session:

Given someone else's ID, call the useJoinPeerSession hook with the type of the state you want to share. The hook returns an array with the following values:

  1. The state of your partner
  2. Your state
  3. A function to set your state (and send it to your partner)
  4. A boolean indicating if you are connected to your partner
const [partnerState, myState, setMyState, isConnected] =
  useJoinPeerSession<StateInterface>(peerID, {
    message: 'Hi there I joined',
    color: '#ff7700',
  });

Keywords

FAQs

Last updated on 09 Jan 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc