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

@liskhq/lisk-p2p

v0.10.0

Published

Unstructured P2P library for use with Lisk-related software

Downloads

928

Readme

@liskhq/lisk-p2p

@liskhq/lisk-p2p is a library for creating unstructured P2P networks. The library in abstraction enables peer discovery, network security, and reliability. A developer can leverage all the underlying specifications proposed in LIP: 0004: Introduce robust peer selection and banning mechanism and use the library for building p2p use cases.

Installation

$ npm install --save @liskhq/lisk-p2p

To run a P2P node.

const { P2P } = require('@liskhq/lisk-p2p');

const p2p = new P2P({
	nodeInfo: {
		port: 5001,
		networkId: '123xyz',
		networkVersion: '1.1',
	},
});

const run = async () => {
	await p2p.start();
};

run()
	.then(() => console.log('P2P node has started!'))
	.catch(err => {
		console.log(`Error occurred while running p2p node: ${err}`);
		p2p.stop();
		process.exit(1);
	});

It will start a node that is available on 5001 websocket port.

P2P node has started!

Actions

It provides simple interface to send, request, broadcast information and many more functions to interact with the network.

  • p2p.start(): start a P2P node after creating an instance.
  • p2p.stop(): stop a P2P node.
  • p2p.config: get the config of the node.
  • p2p.isActive: check the status if the node is up and running.
  • p2p.nodeInfo: check the node status and information.
  • applyNodeInfo(nodeInfo: P2PNodeInfo): broadcast updated nodeInfo to the network.
  • p2p.getConnectedPeers(): get all the connected peers that are connected to your node in the network.
  • p2p.getDisconnectedPeers(): get all the disconnected peers that are part of the network but not connected to you.
  • p2p.request(packet: P2PRequestPacket): request information from the network that will run the peer selection and finds an appropriate peer for you to request information.
  • p2p.send(message: P2PMessagePacket): sends information to 16 connected peers chosen by peer selection for send.
  • p2p.broadcast(message: P2PMessagePacket): broadcast information to all the connected peers.
  • p2p.requestFromPeer(packet: P2PRequestPacket,peerId: string): request from a specific peers in the network.
  • p2p.sendToPeer(message: P2PMessagePacket, peerId: string): sends information to a specific peer in the connected peers.

Events

Listen to various events on the network to observe the network activities more closely and take appropriate actions if needed.

// When a peer updates its information
p2p.on(EVENT_UPDATED_PEER_INFO, (peerInfo: P2PPeerInfo) => {
	// Take any action based peer update event
});
// When a peer sends any information
p2p.on(EVENT_MESSAGE_RECEIVED, (message: P2PMessagePacket) => {
	// Take any action based on message received
	const { event, data, peerId } = message;
});
// When a peer requests any information
p2p.on(EVENT_REQUEST_RECEIVED, async (request: P2PRequest) => {
	// Take any action based on request received and respond with `end(results)` with results or return an error by `error(new Error('Request was not processed successfully'))`
	const { procedure, data, peerId, end, error } = request;
});

Available events

  • EVENT_BAN_PEER
  • EVENT_CLOSE_INBOUND
  • EVENT_CLOSE_OUTBOUND
  • EVENT_CONNECT_ABORT_OUTBOUND
  • EVENT_CONNECT_OUTBOUND
  • EVENT_DISCOVERED_PEER
  • EVENT_FAILED_PEER_INFO_UPDATE
  • EVENT_FAILED_TO_ADD_INBOUND_PEER
  • EVENT_FAILED_TO_COLLECT_PEER_DETAILS_ON_CONNECT
  • EVENT_FAILED_TO_FETCH_PEER_INFO
  • EVENT_FAILED_TO_FETCH_PEERS
  • EVENT_FAILED_TO_PUSH_NODE_INFO
  • EVENT_FAILED_TO_SEND_MESSAGE
  • EVENT_INBOUND_SOCKET_ERROR
  • EVENT_MESSAGE_RECEIVED
  • EVENT_NETWORK_READY
  • EVENT_NEW_INBOUND_PEER
  • EVENT_OUTBOUND_SOCKET_ERROR
  • EVENT_REMOVE_PEER
  • EVENT_REQUEST_RECEIVED
  • EVENT_UPDATED_PEER_INFO

Examples

Check examples folder for a few examples to demonstrate P2P library usage with some use cases. Please make sure you run npm run build before running any example.

  • echo: This example will run nodes that will connect to each other and say "hi" to each other that will be responded by peers when they receive the message. The network will shut down on its own after reaching 10 greetings. Run node examples/echo.
  • find-city-game: Each node will assign themselves a city and change their city randomly and they will update other nodes which city they are currently in. If any two nodes find out that they are in the same city they will broadcast it to others and shutdown. Run node examples/find-city-game.
  • Connect to Lisk networks: Example to create a lightweight p2p client that can connect to the lisk network, listen to various events and request data from the network. Run node examples/lisk-network {NETWORK_NAME} where NETWORK_NAME can be testnet, devnet and mainnet.

License

Copyright 2016-2020 Lisk Foundation

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.