npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-simple-peer

v0.1.3

Published

React component which aims to wrap up [simple-peer](https://github.com/feross/simple-peer) into an easy to use P2P room creator. It's main purpose is to hide signaling data exchange complexity behind a rather simple set of components.

Downloads

44

Readme

react-simple-peer

React component which aims to wrap up simple-peer into an easy to use P2P room creator. It's main purpose is to hide signaling data exchange complexity behind a rather simple set of components.

demo

How does it work ?

sequence

Getting Started

Installing

react-simple-peer comes as a npm package

npm install react-simple-peer

or

yarn add react-simple-peer

Usage

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import {SignalData} from 'simple-peer';
import {User} from "./User";
import io from 'socket.io-client';
import {
    Setup,
    JoinRequest,
    ClientOffer,
    State,
    setModel,
    getId,
    setRoom,
    Peers
} from 'react-simple-peer';

const App = () => {
    const ws = io('localhost:9000');

    return (
        <Setup
            onOfferRequest={processOfferRequest => {
                ws.on('offer-request', (request: JoinRequest) => processOfferRequest(request));
            }}

            emitOfferResponse={(joinRequest: JoinRequest, signalData: SignalData) => {
                ws.emit('offer-response', joinRequest, signalData);
            }}

            onJoinResponse={processJoinResponse => {
                ws.on('join-response', (offer: SignalData, id: string, roomCreatorId: string) => processJoinResponse(offer, id, 'AZE', roomCreatorId))
            }}

            emitJoinAck={(signalData, room, recievedId, id) => {
                ws.emit('join-ack', {offer: signalData, roomId: room, peerId: recievedId}, id);
            }}

            onClientOffer={processClientOffer => {
                ws.on('client-offer', (data: ClientOffer, peerId: string, sessionInitiator: boolean, emitterPeerId: string) => processClientOffer(data, peerId, sessionInitiator, emitterPeerId));
            }}

            emitInitiatorOffers={(offers, id, room) => {
                ws.emit('initiator-offers', offers, id, room);
            }}

            onLeaving={(processLeaving: (id: string) => void) => {
                ws.on('leaving', (id: string) => processLeaving(id));
            }}
        >
            <div className="App">
                <header className="App-header">

                    <input type="text" onChange={event => setModel(new User(event.target.value))}/>

                    <p>
                        State : <State/>
                    </p>

                    <button onClick={() => {
                        const roomId = 'myroom';
                        const room = {initiatorPeerId: getId(), roomId: roomId};
                        setRoom(roomId);
                        ws.emit('create', room);
                    }}>Create a room
                    </button>

                    <button onClick={() => {
                        setRoom('myroom');
                        ws.emit('join-request', {roomId: 'AZE', peerId: getId()});
                    }}>Join a room
                    </button>

                    <Peers>
                        {
                            peers => {
                                return <ul>
                                    {Array.from<any>(peers).map((set: [string, User]) => <li
                                        key={set[1].username}>{set[1].username}</li>)}
                                </ul>
                            }
                        }
                    </Peers>
                </header>
            </div>
        </Setup>
    );
};

ReactDOM.render(<App/>, document.getElementById('root'));

API

<Setup>

Returns :

children

Props

onOfferRequest

Type:

(processOfferRequest: (request: JoinRequest) => void) => void

Wrapper around OfferRequest event listener that provides processing callback for that request.

emitOfferResponse

Type:

(joinRequest: JoinRequest, signalData: SignalData) => void

Wrapper around OfferRequest event emitter that provides both JoinRequest and SignalingData objects.

onJoinResponse

Type:

(processJoinResponse: (offer: SignalData, id: string, roomCreatorId: string) => void) => void

Wrapper around JoinResponse event listener that provides processing callback for that response.

emitJoinAck

Type:

(signalData: SignalData, room: string, recievedId: string, id: string) => void

Wrapper around JoinAck event emitter that provides SignalingData as well as room id, received peer id and self peer id.

onClientOffer

Type:

(processClientOffer: async (data: ClientOffer, peerId: string, sessionInitiator: boolean, emitterPeerId: string) => void) => void

Wrapper around clientOffer event listener that provides processing callback for that offer.

emitInitiatorOffers

Type:

(offers, id, room) => void

Wrapper around InitiatorOffers event emitter that provides list of offers, self id and room id

onLeaving

Type:

(processLeaving: (id: string) => void) => void

Wrapper around leaving event listener that provides processing callback for that leaving peer.

<JoinRoomButton>

Returns :

React.HTMLButtonElement

Props

className

Type:

string

CSS class names

className

Type:

string

CSS class names

room

Type:

string

id of room to join

join

Type:

() => void

Wrapper around joinRequest emitter

<Peers>

Returns :

children

children

Type:

(peers: ReactSimplePeerModel[]) => React.HTMLElement

<State>

Returns :

'Waiting for peers' | 'Emitting offer' | 'Requesting access' | 'Receiving response access' | 'Connecting to existing peers' | 'Joining session'

Returns the current state of the peer

JoinRequest

roomId: string;
peerId: string;

ClientOffer

offer: SignalData;
peers: {[key: string]: string}

setModel

Params:

model: ReactSimplePeerModel

Returns :

void

getId

Returns :

string

Returns self peerId

setRoom

Params:

roomId: string

Returns :

void

<PeerVideo>

Props

peer

Type:

ReactSimplePeerModel

Returns :

React.HTMLVideoElement