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

peer-connector

v0.8.9

Published

A module to accept and request WebRTC multi connections by using WebSockets.

Downloads

12

Readme

peer-connector npm

A module to accept and request WebRTC multi connections by using WebSockets. Simple WebRTC video/voice/screen and data channels.

Installing

$ npm install peer-connector

Demo(sample test)

$ git clone https://github.com/goldenthumb/peer-connector.git
$ cd peer-connector
$ npm install
$ npm run dev

Now open this URL in your browser: http://localhost:3000/

Usage (basic)

Please refer to the file. (example/src/index.js)

import { 
    PeerConnector, 
    getMediaStream, 
    connectWebsocket, 
    Signal,
} from 'peer-connector';

(async () => {
    const stream = await getMediaStream({ video: true, audio: true });
    const peerConnector = new PeerConnector({ stream });
    const websocket = await connectWebsocket('ws://localhost:1234');
    const signal = new Signal({ websocket });

    signal.autoSignal(peerConnector);

    peerConnector.on('connect', (peer) => {
        // peer is generated each time WebRTC is connected.
        console.log('stream', peer.remoteStream);

        peer.on('open', (data) => {
            peer.send('open data channel');
        });
      
        peer.on('data', (data) => {
            console.log('data channel message', data);
        });

        peer.on('close', (data) => {
            console.log('close', data);
        });
    });
})();

Usage (custom)

You can implement the signaling logic as you wish. (Using websocket and MQTT or other)

import { 
    PeerConnector, 
    Peer, 
    getMediaStream, 
    connectWebsocket, 
    Signal, 
    SIGNAL_EVENT 
} from 'peer-connector';

(async () => {
    const stream = await getMediaStream(mediaType);
    const peerConnector = new PeerConnector();
    const websocket = await connectWebsocket('ws://localhost:1234');
    const signal = new Signal({ websocket });

    signal.send(SIGNAL_EVENT.JOIN);

    signal.on(SIGNAL_EVENT.JOIN, ({ sender }) => {
        signal.send(SIGNAL_EVENT.REQUEST_CONNECT, { receiver: sender });
    });

    signal.on(SIGNAL_EVENT.REQUEST_CONNECT, async ({ sender }) => {
        const peer = new Peer({ id: sender, stream });
        peerConnector.addPeer(peer);

        peer.createDataChannel(peerConnector.channelName);

        peer.on('iceCandidate', (candidate) => {
            signal.send(SIGNAL_EVENT.CANDIDATE, { 
                receiver: peer.id, 
                candidate, 
            });
        });

        signal.send(SIGNAL_EVENT.SDP, { 
            receiver: peer.id, 
            sdp: await peer.createOfferSdp(), 
        });
    });

    signal.on(SIGNAL_EVENT.SDP, async ({ sender, sdp }) => {
        if (sdp.type === 'answer') {
            const peer = peerConnector.getPeer(sender);
            await peer.setRemoteDescription(sdp);
        } else {
            const peer = new Peer({ id: sender, stream });
            peerConnector.addPeer(peer);

            peer.on('iceCandidate', (candidate) => {
                signal.send(SIGNAL_EVENT.CANDIDATE, { 
                    receiver: peer.id, 
                    candidate, 
                });
            });

            await peer.setRemoteDescription(sdp);
            signal.send(SIGNAL_EVENT.SDP, { 
                receiver: peer.id, 
                sdp: await peer.createAnswerSdp() 
            });
        }
    });

    signal.on(SIGNAL_EVENT.CANDIDATE, ({ sender, candidate }) => {
        const peer = peerConnector.getPeer(sender);
        peer.addIceCandidate(candidate);
    });

    peerConnector.on('connect', (peer) => {
        peer.on('open', (data) => {
            peer.send('open data channel');
        });
      
        peer.on('data', (data) => {
            console.log('data channel message', data);
        });

        peer.on('close', (data) => {
            console.log('close', data);
        });
    });
})();

API

getMediaStream

/**
 * @param {{ screen?: boolean, video?: boolean, audio?: boolean  }} options
 * @return {Promise<MediaStream>}
*/
const stream = await getMediaStream(options);

// Desktop screen sharing. 
const stream = await getMediaStream({ screen: true });

// Video and audio sharing.
const stream = await getMediaStream({ video: true, audio: true });

PeerConnector

/**
 * @param {object} [options]
 * @param {MediaStream} [options.stream]
 * @param {RTCConfiguration} [options.config]
 * @param {boolean} [options.channel]
 * @param {string} [options.channelName]
 * @param {RTCDataChannelInit} [options.channelConfig]
 */
const peerConnector = new PeerConnector();
{   
    stream: false,
    config: { iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] },
    channel: true,
    channelName: '<random string>',
    channelConfig: {},
}

| Name | type | Description | |------------|--------|------------------------------| | stream | prop | media local stream | | peers | prop | connected peers | | addPeer | method | add peer | | removePeer | method | remove peer | | hasPeer | method | has peer | | getPeer | method | get peer | | close | method | close media local stream | | destroy | method | removes all listeners | | connect | event | triggers when connect WebRTC |

Peer

/**
 * @param {object} props
 * @param {string} [props.id]
 * @param {MediaStream} [props.stream]
 * @param {RTCConfiguration} [props.config]
 * @param {boolean} [props.channel]
 */
const peer = new Peer();
{   
    id: '<random string>',
    stream: false,
    config: { iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] },
    channel: true,
}

| Name | type | Description | |----------------------|--------|----------------------------------------------------| | id | prop | peer id | | localSdp | prop | local sdp | | localStream | prop | local media stream | | remoteSdp | prop | remote sdp | | remoteStream | prop | remote media stream | | createOfferSdp | method | create offer and set local sdp | | createAnswerSdp | method | create answer and set local sdp | | createDataChannel | method | create data channel | | setRemoteDescription | method | set remote sdp | | addIceCandidate | method | add ice candidate | | send | method | send data using data channel | | close | method | peer close | | destroy | method | removes all listeners | | iceCandidate | event | triggers when candidates occur | | changeIceState | event | triggers when oniceconnectionstatechange occur | | open | event | triggers when data channel open | | data | event | data received by data channel | | close | event | triggers when ICE connection or data channel close | | error | event | triggers when data channel error |

Signal

/**
 * @param {object} props
 * @param {WebSocket} props.websocket
 * @param {string} [props.id]
 */
const signal = new Signal();
{   
    id: '<random string>',
}

| Name | type | Description | |----------------|--------|-------------------------------| | id | prop | signal id | | send | method | send message | | autoSignal | method | auto signaling | | destroy | method | removes all listeners | | join | event | triggers When user join | | requestConnect | event | triggers when connect request | | sdp | event | triggers when user sdp | | candidate | event | triggers when user candidate |

License

MIT