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

udp-node

v2.1.0

Published

Find and communicate with network nodes over UDP.

Downloads

49

Readme

udp-node Build Status

Find and communicate with network nodes over UDP.

About

udp-node provides a programatic way of discovering and interacting with other nodes over UDP. This interaction can be easily achieved using provided methods, like ping, broadcast and onNode and you can also create your custom events using the on and send methods.

Install

npm install --save udp-node

Sample

In the sample folder you'll find the basics of finding nodes and sending custom messages. The code is prepared to run on the same machine by changing default port on one node.

Find all nodes

In this example we will send a broadcast message to find all nodes in the network. These nodes are other instances of udp-node.

const UdpNode = require('udp-node')
const six = new UdpNode()
six
  .set({
    name: 'Six',
    type: 'machine'
  })
  .broadcast()
  .onNode((message, rinfo) => {
    // FOUND NODE
    // message: contains node's name, type and other details set when node was initialized using set()
    // rinfo: contains node's ip address and port
  })

This broadcast message is sent to the default port, 3024. Nodes listening on that port will automatically answer with a pong message.

Find specific nodes

When a node is created it can set a type. Then, you can send a broadcast message filtering by type. Nodes belonging to that type are the only ones who will answer with a pong.

const UdpNode = require('udp-node')
const six = new UdpNode()
six
  .set({
    name: 'Six',
    type: 'machine'
  })
  .broadcast({
    filter: ['human']
  })
  .onNode((message, rinfo) => {
    // FOUND HUMAN NODE
  })

Properties

guid

A guid is automatically set for each node when the node is constructed. This means that it will change each time you do new UdpNode().

Methods

set(params)

Initializes udp-node. Must be called to start udp client.

Params object:

  • name: string, node's name
  • type: string, node's type; used on broadcast filter
  • port: int, default is 3024
  • broadcastAddress: string, default is 255.255.255.255
  • logLevel: calls setLogLevel with passed value

broadcast(params)

Sends a broadcast message to the network. Other udp-nodes will respond with a pong message containing their identity.

Params object:

  • filter: array, used to get a pong from specific node types
  • port: int, default is 3024
  • address: string, default is 255.255.255.255
  • data: object, custom data to be included in broadcast message

ping(params)

Sends a ping message to a specific udp-node. If that node is available it will respond with a pong message containing its identity.

Params object:

  • address: string, required; the address of the node we want to ping; example: 192.168.1.123
  • port: int, default is 3024
  • data: object, custom data to be included in ping message

send(message, callback)

Sends a custom message.

message properties:

  • type: string, required; this is the message type, not to be confused with node's type
  • port: int, default is 3024
  • address: string, default is 255.255.255.255

After the message is sent passed callback is called.

onNode(callback)

Called when a node of interest if found, either by a response to our broadcast or ping or when the other node send us a broadcast or ping.

on(type, callback)

Adds a listener for a custom message. When a message of the specified type is recieved calls the passed callback.

CHANGE from previous version:

Returns a ref to this to allow chaining.

Params:

  • type: string, required; any string that identifies the message type
  • callback: function, required; called each time a message of specified type is received

off(type, index)

Turns off individual listeners or all listeners for passed message type when index is not provided.

Params:

  • type: string, required; any string that identifies the message type
  • index: int, identifies a specific listener

setLogLevel(level)

udp-node uses Winston for logging. Please, refer to logging levels on the official documentation for more details.

Params:

  • level: string, required; one of: error, warn, info, verbose, debug, silly

close(callback)

Closes UDP socket. Should always be called after finished working with the udp-node to ensure the socket is closed and the port freed up. When the socket is closed calls passed callback.

Notes

If you want to run multiple nodes on the same machine you'll need to provide different ports to each one. You can easily do this by passing the port property when calling set() on each node.