@thebigsasha/react-peerjs-hooks
v0.0.15
Published
Simple implementation of hooks for using PeerJS in a React App, compatible ssr and pre-compilers like NextJS and Gatsby
Downloads
9
Maintainers
Readme
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:
- The state of the partner
- The state of the host
- A function to set the state of the host
- A boolean indicating if the host is connected to the partner
- 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:
- The state of your partner
- Your state
- A function to set your state (and send it to your partner)
- 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',
});