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

nanosockets-js

v0.0.2

Published

Bindings for NanoSockets in Node.js

Downloads

3

Readme

nanosockets-js

nanosockets-js is a lightweight and efficient library for managing low-level socket connections in Node.js, based on the powerful NanoSockets library. This project uses napi-rs to expose a simple, fast, and efficient network interface for JavaScript developers, allowing communication via UDP sockets.

| (index) | Server | Avg Messages/sec | % Difference | |---------|-------------------------|------------------|--------------| | 0 | Nanosockets C# | 41,706 | 0.00% | | 0 | Nanosockets Node.js | 37,691 | 0.00% |

Manual Build

To manual build the library:

rm -rf build
node-gyp clean
node-gyp configure
node-gyp build

Installation

To install the library via npm, run:

npm install nanosockets-js

Features

  • Based on the native low-level NanoSockets library.
  • High-performance UDP socket support.
  • Native integration with Node.js using Rust and napi-rs.
  • Simple APIs for creating, sending, and receiving messages via UDP.
  • Support for IPv4 and IPv6 and dynamic ports.

Usage Example

Here is a basic example of how to use the UDP API provided by the library:

const { UDP, Address } = require('nanosockets-js');

UDP.initialize();

const server = UDP.create(256 * 1024, 256 * 1024);

const address = Address.createFromIpPort('::0', 5001);

if (UDP.bind(server, address) == 0)
    console.log("Socket bound!");

if (UDP.setDontFragment(server) != 0)
    console.log("Don't fragment option error!");

if (UDP.setNonBlocking(server) != 0)
    console.log("Non-blocking option error!");

while (true) {
    const pollResult = UDP.poll(server, 15);
    
    if (pollResult > 0) {
        let { bytesReceived } = UDP.receive(server, 256 * 1024);

        console.log(`Message received from - IP: ${bytesReceived.address}:${bytesReceived.port}, Data: ${bytesReceived.data.toString()}`);
        UDP.send(server, Address.createFromIpPort(bytesReceived.address, bytesReceived.port), bytesReceived.data);
    }
}

UDP.destroy(server);
UDP.deinitialize();

API

UDP.initialize()

Initializes the UDP subsystem. Must be called before any other UDP operations.

UDP.create(sendBufferSize, receiveBufferSize)

Creates a UDP socket with the specified buffer sizes.

  • sendBufferSize (Number): The size of the send buffer.
  • receiveBufferSize (Number): The size of the receive buffer.

Address.createFromIpPort(ip, port)

Creates a UDP address from an IP and port.

  • ip (String): The IPv4 address.
  • port (Number): The port associated with the address.

UDP.send(socket, address, buffer)

Sends data to a specific address using the UDP socket.

  • socket (Object): The created UDP socket.
  • address (Object): The destination address (an instance of Address).
  • buffer (Buffer): The data to be sent.

UDP.receive(socket, bufferSize)

Receives data from a UDP socket.

  • socket (Object): The created UDP socket.
  • bufferSize (Number): The maximum buffer size for receiving data.

Returns an object with the following properties:

  • bytesReceived: The number of bytes received.
  • buffer: The buffer containing the received data.

UDP.destroy(socket)

Destroys the UDP socket and frees associated resources.

UDP.deinitialize()

Finalizes the UDP subsystem. Should be called after all networking operations to free resources.

Contribution

Contributions are welcome! If you encounter issues or have suggestions for improvements, feel free to open an issue or submit a pull request. This project is based on the NanoSockets library, and any enhancements are greatly appreciated.

License

This project is licensed under the MIT License. See the LICENSE file for more details.