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

socketpeer

v1.1.0

Published

simple 1:1 messaging via WebRTC Data Channels and WebSockets

Downloads

13

Readme

socketpeer

Simple 1:1 messaging via WebRTC Data Channels and WebSockets.

Read this great article.

Features

  • concise, Node.js-style API for WebRTC
  • simple 1:1 peer connection signalling, pairing, and messaging
  • fallback to WebSockets if WebRTC Data Channels are unsupported
  • automatic reconnection if peer connections prematurely close

This module works great in the browser with browserify.

NOTE: If you are not using browserify, then use the included standalone file socketpeer.min.js. This exports a SocketPeer function on window.

Installation

To install from npm:

npm install socketpeer

To install the Node dependencies from the git repository:

npm install

Development

To run the browserify watcher (files are output to the build/ directory):

npm run watch

To create the browserify bundles (files are output to the build/ directory):

npm run build

Testing

Set these up locally:

To run tests locally:

npm run test-local

To run tests in the cloud:

npm test

Client API

peer = new SocketPeer([opts])

Create a new peer WebRTC Data Channel peer connection (only WebRTC if socketFallback is false).

A "data channel" for text/binary communication is always established, because it's cheap and often useful.

If opts is specified, then the default options (shown below) will be overridden.

{
  pairCode: '<random string>',
  socketFallback: true,
  socket: [Object],
  url: 'http://localhost',
  reconnect: true,
  reconnectDelay: 1000,
  timeout: 10000,
  autoconnect: true,
  serveLibrary: true
}

The options do the following:

  • pairCode - string used to identify peers
  • socketFallback - when true, falls back to WebSockets when WebRTC is unavailable
  • socket - custom instance of a WebSocket connection to reuse
  • url - URL to WebSocket server
  • reconnect - when true, reconnects if peer connection drops
  • reconnectDelay - if reconnect is set, how long to (ms) wait before reconnecting
  • timeout - how long to wait (ms) before abandoning connection
  • autoconnect - when true, automatically connects upon page load
  • serveLibrary - when true, serves library at /socketpeer/socketpeer.js

peer.connect()

If reconnect or autoconnect is false, manually start the connection.

peer.send(data)

Send data to the remote peer.

peer.on(event, listener)

Adds a listener to the end of the listeners array for the specified event.

peer.off(event)

Remove listeners for the specified event.

SocketPeer extends Node's EventEmitter. See the docs for the remaining methods.

peer.close()

Destroy and cleanup this peer connection.

Events

peer.on('connect', function () {})

Fired when the peer connection and/or data channel is established.

peer.on('connect_error', function (data) {})

Fired when a connection error occurs.

peer.on('connect_timeout', function (data) {})

Fired when a connection timeout occurs.

peer.on('data', function (data) {})

Received a message from the remote peer.

peer.on('reconnect', function (data) {})

Fired when a reconnection occurs.

peer.on('reconnect_error', function (data) {})

Fired when a reconnection error occurs.

peer.on('reconnect_timeout', function (data) {})

Fired when a reconnection timeout occurs.

peer.on('upgrade', function (data) {})

Fired when a connection is successfully upgraded from WebSocket to RTCDataChannel.

peer.on('upgrade_attempt', function (data) {})

Fired when an upgrade attempt occurs.

peer.on('upgrade_error', function (data) {})

Fired when an upgrade error occurs.

peer.on('downgrade', function (data) {})

Fired when a connection falls back to WebSockets.

peer.on('close', function () {})

Called when the peer connection has closed.

peer.on('busy', function (err) {})

Fired when two clients are already connected using a same pair code. err is an Error object.

peer.on('error', function (err) {})

Fired when an error occurs. err is an Error object.

Server API

peerServer = new SocketPeerServer([opts])

Create a new server for establishing peer connections (i.e., "signalling") and passing WebSocket messages through (if WebRTC Data Channel not supported).

If httpServer is specified, that existing server will be used instead and a ws.Server will be created and attached to it. To use an existing ws.Server for signalling, pass wsServer.

If opts is specified, then the default options (shown below) will be overridden.

{
  allowedOrigins: [Array],
  httpServer: undefined,
  wsServer: undefined,
  peerTimeout: 60000,
  pairCodeValidator: function (pairCode) {}
}

The options do the following:

  • allowedOrigins - array of allowed origins (optional)
  • peerTimeout - how long to wait before abandoning peer connection (defaults to 6000 ms, 1 minute)
  • pairCodeValidator - function that allows custom validation on the pairCode passed from the client (optional)

peerServer.socket

A property that links to the instance of ws.Server.

peerServer.server

A property that links to the instance of http.Server.

peerServer.leave(pairCode)

Breaks both ends of a peer connection (WebSocket or WebRTC).

Licence

MIT Licence

Contributing

Contributions are very welcome!