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

mudpjson

v0.0.8

Published

Module with classes to send and receive data as JSON over UDP

Downloads

3

Readme

mUDPJSON

Module with classes to send and receive data as JSON over UDP.

Getting Started

  1. Install mUDPJSON via NPM.

npm install mudpjson

Optionally: rename mudpjson to mUDPJSON: npm is unable to handle the complexity of uppercase characters in a module name. Node.js on Windows does not have this problem, so renaming the folder is not required for you to use the module.

  1. Require mUDPJSON in your project.

var mUDPJSON=require("mUDPJSON");

  1. Instantiate a cReceiver to receive data.
var oUDPJSONReceiver = new mUDPJSON.cReceiver();
oUDPJSONReceiver.on("message", function (oError, xData) {
  if (oError) {
    // Handle oError
  } else {
    // Process xData
  }
});
  1. Instantiate a cSender to send data.
var oUDPJSONSender = new mUDPJSON.cSender();
oUDPJSONSender.on("start", function() {
  oUDPJSONSender.fSendMessage("Anything that can be stringified", function (oError) {
    if (oError) {
      // Handle oError
    }
  });
});

Notes

The cSender and cReceiver instances can be on the same machine or on two different machines. By default cSender broadcasts to all machines on the local subnet.

cSender.fSendMessage is used to send a message that consist of one value converted to a string using JSON.stringify. cReveiver emits one message event for each such message received, with two parameters. The first parameter is an Error object if an invalid message was received or undefined if the message was valid. The second parameter is the value that was sent, reconstructed from the data in the message using JSON.parse.

Protocol

The protocol used to transmit data is simple and robust. Each message sent starts with a string that represents the length of the JSON data in the message, followed by a semi colon. This is followed by the actual JSON data and another semicolon. Messages that are larger than the maximum transfer unit (MTU) of the network are broken into smaller chunks. However, to reduce the risk of a sender swamping a receiver with data, there is a limit of 1Mb on the size of the JSON data that can be transmitted in one message.

Example:

14;"Hello, World";

Where "Hello, World" is of course 14 characters long. If a message is received that is not in accordance with the protocol, the receiver emits an "error" event.

API

class cSender

Can be used to send values as JSON to one or more receivers.

Constructors:

[new] mUDPJSON.cSender(Object dxOptions);

Where dxOptions is an object that can have the following properties:

  • Number uIPVersion: IP version to use (valid values: 4 (default), 6).
  • String sHostname: Target computer (default: broadcast to local subnet).
  • Number uPort: port number to send to (default: 28876).
  • Number uMTU: Maximum Transfer Unit (default: undefined - the code will attempt to determine the value automatically if needed).

Events:

error, parameter: Error oError

Emitted when there is a network error.

start

Emitted when the cSender instance is ready to send messages. You do not need to wait until this event is emitted before sending messages: these are queued until the cSender instance is ready to send them.

stop

Emitted when the cSender instance has stopped sending messages. This happens when there is a network error or after you tell the sender to stop.

Methods:

undefined fSendMessage(Any xMessage, Function fCallback)

Convert the data in xMessage to a string using JSON.stringify and send it over UDP. fCallback(Boolean bSuccess) is called when the message has been sent (bSuccess == true) or when there was an error (bSuccess == false).

undefined fStop()

Stop the cSender instance.

class cReceiver

Used to receive values as JSON data over UDP.

Constructors:

[new] mUDPJSON.cReceiver(Object dxOptions);

Where dxOptions is an object that can have the following properties:

  • Number uIPVersion: IP version to use (valid values: 4 (default), 6).
  • String sHostname: Network device to bind to (default: computer name, use localhost if you want to receive messages only from cSender instances on the same machine).
  • Number uPort: port number to receive messages on (default: 28876).

Events:

error, parameter: Error oError

Emitted when there is a network error.

start

Emitted when the cReceiver instance is ready to receive messages.

message, parameters: Error oError, Any xData

Emitted when the cReceiver instance has received a message. If the message was invalid, oError will contain a description of the problem. Otherwise, oError will be undefined and xData will contain the data sent by the cSender instance.

stop

Emitted when the cReceiver instance has stopped receiving messages. This happens when there is a network error or after you tell the receiver to stop.

Methods:

undefined fStop()

Stop the cReceiver instance.


License

This code is licensed under CC0 v1.0 Universal.