Skip to content

A React component library that makes it easy to add p2p communication into components via LioWebRTC.

Notifications You must be signed in to change notification settings

lazorfuzz/react-liowebrtc

Repository files navigation

react-liowebrtc

A React component library that makes it easy to add p2p communication into components via LioWebRTC.

NPM

Install

npm i react-liowebrtc --save

Or

yarn add react-liowebrtc

Demo

https://react-liowebrtc.netlify.com

Usage

Example Component (Data Channels only)

importReact,{Component}from'react';import{LioWebRTC}from'react-liowebrtc'importMyComponentfrom'./MyComponent';classExampleextendsComponent{handlePeerData=(webrtc,type,payload,peer)=>{if(type==='event-label'){console.log(payload);}}render(){return(<LioWebRTCoptions={{dataOnly: true}}onReady={this.joinRoom}onCreatedPeer={this.handleCreatedPeer}onReceivedPeerData={this.handlePeerData}onRemovedPeer={this.handlePeerLeft}><MyComponent/></LioWebRTC>)}}

MyComponent

importReact,{Component}from'react';import{withWebRTC}from'react-liowebrtc';classMyComponentextendsComponent{handleClick=()=>this.props.webrtc.shout('event-label','payload');render(){return(<div><buttononClick={this.handleClick}> Click Me </button></div>);}}exportdefaultwithWebRTC(MyComponent);

Video Chat Example

importReact,{Component}from'react';import{LioWebRTC,LocalVideo,RemoteVideo}from'react-liowebrtc'classExampleVideoChatextendsComponent{constructor(props){super(props);this.state={peers: []};}join=(webrtc)=>webrtc.joinRoom('video-chat-room-arbitrary-name');handleCreatedPeer=(webrtc,peer)=>{this.setState({peers: [...this.state.peers,peer]});}handleRemovedPeer=()=>{this.setState({peers: this.state.peers.filter(p=>!p.closed)});}generateRemotes=()=>this.state.peers.map((peer)=>(<RemoteVideokey={`remote-video-${peer.id}`}peer={peer}/>));render(){return(<LioWebRTCoptions={{debug: true}}onReady={this.join}onCreatedPeer={this.handleCreatedPeer}onRemovedPeer={this.handleRemovedPeer}><LocalVideo/>{this.state.peers&&this.generateRemotes()}</LioWebRTC>)}}exportdefaultExampleVideoChat;

Component Props

LioWebRTC Component

LioWebRTC.propTypes={options: PropTypes.object,// Initializing options passed into the liowebrtc libraryonReady: PropTypes.func,// Event listenersonJoinedRoom: PropTypes.func,// When we successfully join a roomonReceivedPeerData: PropTypes.func,// When we receive a shout or whisper from a peeronChannelOpen: PropTypes.func,// When a new data channel is established with a peeronConnectionReady: PropTypes.func,// When the signaling connection is readyonCreatedPeer: PropTypes.func,// When a new peer connectsonPeerStreamAdded: PropTypes.func,// When a peer media stream is addedonPeerStreamRemoved: PropTypes.func,// When a peer media stream is removedonIceConnectionStateChange: PropTypes.func,// When the connection state with a peer changesonSignalingStateChange: PropTypes.func,// When the connection to the signaling server changesonLeftRoom: PropTypes.func,// When exited the roomonPeerMute: PropTypes.func,// When a peer mutes themselvesonReceivedSignalData: PropTypes.func,// When we get a message via the signaling server from a peeronPeerUnmute: PropTypes.func,// When a peer unmutes themselvesonRemovedPeer: PropTypes.func,// When a peer disconnects from usonConnectionError: PropTypes.func// When an error occurs in connecting to a peer};

All event emitters pass a webrtc session manager object to the listener functions. For example, the onReceivedPeerData event passes the following objects: (webrtc, type, data, peer). The onCreatedPeer event passes (webrtc, peer). Take a look at the LioWebRTC docs for more information on LioWebRTC's events and methods. All events emitted by LioWebRTC will have a preceding webrtc object when using react-liowebrtc.

LocalVideo Component

LocalVideo.propTypes={videoProps: PropTypes.object,// props passed to the inner video element};

RemoteVideo Component

RemoteVideo.propTypes={videoProps: PropTypes.object,// props passed to the inner video elementpeer: PropTypes.instanceOf(Peer)// the Peer instance};

These props are needed to initialize and set event listeners for the liowebrtc library. Take a look at the liowebrtc docs for more info.

For more info, please take a look at this tutorial showing how to build a chat room with react-liowebrtc.

License

MIT © lazorfuzz

About

A React component library that makes it easy to add p2p communication into components via LioWebRTC.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •