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

v1.2.1

Published

Lightweight JSON UDP socket API written in JavaScript for NodeJS

Downloads

63

Readme

udp-json

Lightweight JSON UDP socket API written in JavaScript for NodeJS. It provides a simple protocol on top of UDP to send/receive JSON objects. It does not handle UDP unreliability.

Table of Contents

Protocol

A UDP datagram consists of a datagram header and a data section. In the data section of each UDP datagram, udp-json adds another header and a new data section. The header consists of 3 fields, each of which is 4 bytes (32 bits):

+----------+----------------------+------------------------+
| ID (4 B) | Datagram count (4 B) | Current datagram (4 B) |
+----------+----------------------+------------------------+
|                                                          |
|                     Partial Data                         |
|                                                          |
+----------+----------------------+------------------------+

ID

This field identifies to which data refers this partial data.

Datagram count

This field specifies the number of UDP datagrams needed to complete the data.

Current datagram

This field identifies this UDP datagram location inside all the UDP datagrams to complete the data in the correct order.

Partial Data

Fragment of JSON stringified data encoded in utf-8.

Example

Example of sending the object { attribute: "dummy" } split in two datagrams.

+----------+----------------------+------------------------+
| ID = 333 | Datagram count = 2   | Current datagram = 0   |
+----------+----------------------+------------------------+
|                                                          |
|                         {attr                            |
|                                                          |
+----------+----------------------+------------------------+

+----------+----------------------+------------------------+
| ID = 333 | Datagram count = 2   | Current datagram = 1   |
+----------+----------------------+------------------------+
|                                                          |
|                    ibute: "dummy"}                       |
|                                                          |
+----------+----------------------+------------------------+

Usage

See examples folder for more info.

const JSONSocket = require('udp-json');

// Listener socket
const socket = dgram.createSocket('udp4');
socket.bind(56554, '127.0.0.1');
const jsonSocket = new JSONSocket(socket)
jsonSocket.on('message-complete', (msg, rinfo) => {
    console.log('Message received', rinfo, msg);
})

// Sender socket
const socket2 = dgram.createSocket('udp4');
const jsonSocket2 = new JSONSocket(socket2, {maxPayload: 496, timeout: 1000});
jsonSocket2.send({ dummy: 'Dummy Text' }, 56554, '127.0.0.1', (e) => {
    if (e) {
        console.log('error', e);
        return;
    }
    console.log('Message sent');
});

API

Here are listed all methods implemented in udp-json.

JSONSocket

JSONSocket()

JSONSocket(Socket[, options]). Creates a JSON socket using the given options.

JSONSocket: options

Available options for creating a socket. It must be an object with the following properties:

| Property | Type | Description | | ------------ | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | | maxPayload | <number> | Max payload size (in bytes). If the resulting byte array exceeds this size, it will be splitted in chunks sent in different datagrams. Default: 496. | | timeout | <number> | Max time (in ms) between receiving a new datagram and the last datagram received. Default: 1000. |

send()

send(obj, port[, address][, callback]). Broadcasts an object on the socket. The destination port must be specified. An optional callback function may be specified to as a way of reporting errors or for determining when all datagrams have been sent.

'message-complete'

The 'message-complete' event is emitted when a new object is available on a socket. The event handler function is passed two arguments: obj and rinfo.

'message-error'

The 'message-error' event is emitted whenever any error occurs. The event handler function is passed a single Error object.

'message-timeout'

The 'message-timeout' event is emitted if a partial message times out from not receiving a new datagram. The event handler function is passed a single Error object.

Maintainers

License

The library is released under the MIT license. For more information see LICENSE.