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

peersockets

v1.0.0

Published

Directly connect to peers via hyperswarm (or an API-compatible alternative).

Downloads

36

Readme

peersockets

Build Status

Warning: Extension messages are currently not MAC'd, so we can't detect tampering. This is still experimental!

Peersockets is a lightweight wrapper around corestore-swarm-networker that lets you exchange topic-tagged messages with peers over hypercore-protocol streams.

To use a Peersocket, you first join a topic -- under the hood, this will register a topic-specific extension on every current peer connection in your networker. By defining topics at the extension level, Peersockets does not need to do any extra encoding/decoding. Once you've created a topic, you can send messages to peers by their NOISE keys. If the remote peer has also joined the topic, they will receive the message.

In the future, it'd be cool to support a broadcast method as well! Supporting topic broadcasts in the v0 version would add complexity, though, and would be challenging to do without leaking topic names to the wrong peers.

Installation

npm i peersockets --save

Example

This example assumes you already have Networker (corestore-swarm-networking) instance. You can use anything that emits handshake and stream-closed events, though!

const networker = (my swarm networker)
const sockets = new Peersockets(networker)

// Let's say you have a friend's NOISE key, and you're already swarming with them.
const friendKey = ...

const handle = sockets.join('my-topic', {
  onmessage: (msg, peer) => {
   console.log(`I got message ${msg} from ${peer.remotePublicKeytoString('hex')}`)
  }
})

// If your friend is also subscribed to topic 'my-topic', they will receive this message.
handle.send(friendKey, 'hello!')

API

const socket = new Peersockets(networker)

Creates a new Peersockets instance.

const handler = socket.join(topicName, opts = { onmessage, onclose })

Joins a new topic. If this is the first time the topic has been joined, a topic extension will be registered on every connected peer. Otherwise the previous topic/extension will be reused.

  • topicName can be any string
  • opts can contain the following callbacks:
    • onmessage(remoteKey, msg) - Called whenever a message with this topic is received from remoteKey.
    • onclose() - Called if the topic is closed via leave.

The topic handler's API is described below.

socket.leave([topicName])

Close all topic handlers for topicName and unregister the topic's extension from all connected peers.

If a topicName is not specified, all topics will be left.

topicHandle.send(msg, peer)

Send a message to peer.

msg is a string or a Buffer. peer is a @corestore/networker Peer instance that can be obtained from networker.peers.

topicHandle.close()

Close the handle when you're finished with it. This will not unregister the topic extension.

License

MIT