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

moshpit

v0.1.1

Published

exchange messages between peers using http+sse

Downloads

7

Readme

moshpit

exchange messages between peers using http+sse.

example

fork a server:

$ moshpit -p 5000

ping and pong:

var pit = require('moshpit')('http://localhost:5000');

var initiator = pit();

initiator.on('connect', function (data) {
  var initiatorId = data.cid;

  initiator
    .on('join', function (data) {
      var peerId = data.cid;

      this.write({
        message: 'ping ' + data.cid,
        from: initiatorId,
        _cid: peerId
      });
    })
    .on('signal', function (data) {
      console.log(data.message);
      this.destroy();
    });

  var peer = pit(data.id);

  peer.on('signal', function (data) {
    console.log(data.message);
    this.write({
      message: 'pong ' + data.from,
      _cid: data.from // omit this to send a broadcast message
    });
  });
});

api

var pit = require('moshpit')(uri)

var peer = pit(id)

Returns a duplex object stream that takes in broadcast (or direct) messages as input and produces rows of [event label, message body] pairs.

This connects to the moshpit http server at given url (optionally at given channel specified by id) and starts a server-sent events stream. Writing to peer makes a POST request to send supplied data.

You can immediately start writing objects to peer, it will buffer up writes until the sse connection is made. When the sse connection is established, the connect event will be emitted.

var pit = require('moshpit')('http://localhost:5000');

var owner = pit();
owner.on('data', console.log.bind(null));
owner.write({ 'hello': 'me' });

outputs:

[ 'connect',
  { id: 'a1ad1a210063392d29027dbdd5fc9dd7',
    cid: 'a31b6b15326ca2907b3b492df1e6385a',
    token: '3b970f792805fd3081b058a5655a534d' } ]
[ 'signal', { hello: 'me' } ]

If id is not supplied, server creates a new channel and assigns an id to it. The peer that created this channel, becomes the initiator or owner. When owner leaves, all connections are closed and the channel is destroyed.

To join an existing channel, you pass id of it:

var peer = pit('a1ad1a210063392d29027dbdd5fc9dd7');
peer.on('data', console.log.bind(null));

outputs:

[ 'connect',
  { id: 'a1ad1a210063392d29027dbdd5fc9dd7',
    cid: 'a31b6b15326ca2907b3b492df1e6385a',
    token: '3b970f792805fd3081b058a5655a534d' } ]

When peer joins, owner is also dispatched a join event with peer's cid. Likewise when it leaves, a leave event will be emitted.

pushing broadcast messages

peer.write({ boom: 'boom' });

pushing direct messages

_cid property is reserved to send direct messages:

peer.write({
  boom: 'boom',
  _cid: 'a31b6b15326ca2907b3b492df1e6385a'
});

events

peer.on('connect', function (data))

Emitted when the sse connection is established. data contains:

  • id of the connected channel
  • cid is connection id that identifies this peer
  • token is a secret value which is internally used to authenticate

peer.on('signal', function (data))

Emitted when a signal is received.

owner.on('join', function (data))

Emitted when a new peer is subscribed. data contains cid of the joining client.

owner.on('join', function (data))

Emitted when a peer leaves, data contains cid of the leaving client.

server

var createApp = require('moshpit/app')

var app = createApp()

Returns a koa application which is a thin wrapper around koa-signal and koa-cors.

usage

usage: moshpit {options}

Options:
  --help, -h     show this message
  --version, -V  show version number
  --port, -p     listen for connections on this port
  --uid, -u      drop permissions to this uid
  --gid, -g      drop permissions to this gid

license

mit