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-api

v0.0.2

Published

An abstraction for RPC and Pub/Sub over a socket.

Downloads

4

Readme

Peer Api

This library helps you create typed abstraction for RPC and Pub/Sub on top of a socket or event-emitter.

Getting Started

npm install peer-api

Suppose you have two processes, A and B, and you have a socket between them. It's quite cumbersome to write event-based code to emit and listen. This code quickly turns into spaghetti.

However, remote procedure calls (RPC) and publish-subscribe (pub/sub) semantics are really convenient for interacting between processes.

This library allows you to define RPC and Pub/Sub interface using TypeScript types and implements all of the messaging under the hood.

So long as you can send an event and listen for events, you can use this library.

import { PeerApi } from "peer-api"

// Process A wants to be able to call rpcB and subscribeB in process B.
type A = {
	rpcB(arg: number): number

// A subscription is defined as a function with a callback and a returned function
	// for unsubscribing.
	subscribeB(cb: (value: number) => void): () => void
}

// Process B wants to be able to call rpcA and subscribeA in process A.
type B = {
	rpcA(arg: string): string
	subscribeA(cb: (value: string) => void): () => void
}

// Wire up each process to communicate with each other.
// We'll use EventEmitter to simulate an actual socket.
const aEvents = new EventEmitter()
const bEvents = new EventEmitter()

const a = new PeerApi<A, B>({
	send: (message) => {
		bEvents.emit("event", message)
	},
	listen: (callback) => {
		aEvents.on("event", callback)
		return () => aEvents.off("event", callback)
	},
})

const b = new PeerApi<B, A>({
	send: (message) => {
		aEvents.emit("event", message)
	},
	listen: (callback) => {
		bEvents.on("event", callback)
		return () => bEvents.off("event", callback)
	},
})


// Define the APIs.
b.answer.rpcB = (x) => x + x
b.publish.subscribeB = (cb) => {
	const timerId = setInterval(() => cb(0), 1)
	return () => clearInterval(timerId)
}

a.answer.rpcA = (x) => x + a
a.publish.subscribeA = (cb) => {
	const timerId = setInterval(() => cb("hello"), 1)
	return () => clearInterval(timerId)
}

// Test it out.
await a.call.rpcB(10)
const unsubscribeB = a.subscribe.subscribeB((value) => {
	// callback
})

await b.call.rpcA("hello")
const unsubscribeA = b.subscribe.subscribeA((value) => {
	// callback
})

Future Work

If this were implemented over a UDP socket or some other kind of lossy network, then we'd need to add some retries and backoffs.