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

@organicdesign/libp2p-message-handler

v0.4.3

Published

A message handler for Libp2p.

Downloads

7

Readme

libp2p-message-handler

A message handler for Libp2p. This package makes it easy to create a protocol on Libp2p by converting the streams into a discrete messaging system.

Table of Contents

Install

npm i @organicdesign/libp2p-message-handler

Usage

import { createMessageHandler } from "@organicdesign/libp2p-message-handler";

const handler = createMessageHandler(options)(libp2p);

await handler.start();

handler.handle((message, peer) => {
	// Handler an incomming message.
});

// Send a message to a peer (will throw if not connected).
await handler.send(message, peer);

// Stop the synchronizer.
await handler.stop();

API

createMessageHandler

createMessageHandler([options])(libp2p);
  • options <Object> An optional object with the following properties:
    • protocol <String> A string which specifies the name of the protocol. Default: "/message-handler/0.0.1".
  • libp2p <Libp2p> The libp2p instance.
  • Returns: <MessageHandler> The message handler instance.

Creates a Libp2p message handler.

MessageHandler

new MessageHandler(libp2p, [options]);
  • options <Object> An optional object with the following properties:
    • protocol <String> A string which specifies the name of the protocol. Default: "/message-handler/0.0.1".
  • libp2p <Libp2p> The libp2p instance.

The MessageHandler class. It is not recommended to instanciate it directly but rather use the createMessageHandler function.

start

messageHandler.start();
  • Returns: <Promise>

Start the message handler, resolves when it has finished starting.

stop

messageHandler.stop();
  • Returns: <Promise>

Stop the message handler, resolves when it has finished stopping.

isStarted

messageHandler.isStarted();
  • Returns: <boolean>

Check if the message handler is started.

send

messageHandler.send(message, peer);
  • message <Uint8Array> The message to send.
  • peer <PeerId> The peer ID of the peer to send the message to.
  • Returns: <Promise>

Send a message to a connected peer. Resolves when the message is sent. Rejects if it fails to send the message.

broadcast

messageHandler.broadcast(message);
  • message <Uint8Array> The message to send.
  • Returns: <Promise> Resolves to a list of <PromiseSettledResult>s.

Send a message to all connected peers. Resolves when the message has been sent or has failed to be sent to all pairs. This method only sends the message to connected pairs, if you are wanting to send a message to the entire network you should use a pubsub module instead.

handle

messageHandler.handle(handlerFunc);
  • handlerFunc <(Uint8Array, PeerId) => void> The handler function to call with the received message and the sender's peer ID.

Handle incomming messages from other peers.

unhandle

messageHandler.unhandle(handlerFunc);
  • handlerFunc <(Uint8Array, PeerId) => void> The handler function which was previously handled.

Stop handling messages with this handler function.

Logging

The logger has the following namespaces:

  • libp2p:message-handler - Logs general actions like starting, stopping and opening streams.
  • libp2p:message-handler:messages - Logs when it sends or receives messages.

To enable logging in nodejs add the following environment variable (by prefixing the start command):

DEBUG=libp2p:message-handler*

Or in the browser:

localStorage.setItem("debug", "libp2p:message-handler*");

Tests

To run the test suite:

npm run test

To-Do

  • [x] Add tests.
  • [x] Add logging.