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-hole-puncher

v1.1.0

Published

UDP hole punching library

Downloads

52

Readme

CircleCI npm

UDP Hole Puncher

Summary

JS library implementing a UDP hole punching protocol to connect two peers located behind NAT devices. Will not work when one or both peers are located behind a symmetric NAT box. In that case, you may need a relay server + a TURN lib (like this one) to facilitate communication between both peers.

Features

  • no rendez-vous server lock in
  • verifies if bidirectional communication is possible
  • can be browserified (to be used in chrome and cordova apps)

Install

npm install udp-hole-puncher

Usage

const dgram = require('dgram');
const UdpHolePuncher = require('udp-hole-puncher');

// peer's public port and address
const peer = {
  port: 1234,
  addr: '1.2.3.4',
};
// local port
const myPort = 5678;

// socket config
const socket = dgram.createSocket('udp4');
socket.on('error', (error) => {...} );
socket.on('message', (message, rinfo) => {...} );
socket.on('listening', () => {
  // puncher config
  const puncher = new UdpHolePuncher(socket);
  // when connection is established, send dummy message
  puncher.on('connected', () => {
    const message = Buffer.from('hello');
    socket.send(message, 0, message.length, peer.port, peer.addr);
  });
  // error handling code
  puncher.on('error', (error) => {
    ...
  });
  // connect to peer (using its public address and port)
  puncher.connect(peer.addr, peer.port);
});

// bind socket
socket.bind(myPort);

API

var puncher = new UdpHolePuncher(socket, args)

Create a new udp-hole-puncher.

socket must be an operational datagram socket.

args specifies some optional config settings, including the maximum request attempts + timeout between every request attempt (ms). Default settings are { maxRequestAttempts: 10, requestTimeout: 500 }

puncher.connect(addr, port)

Try to establish a connection with a peer using its public address and port. Note that to setup bidirectional communication, both peers must simultaneously execute a connect operation (initiating the punching protocol).

puncher.close()

End execution of the hole punching protocol.

Events

puncher.on('connected', () => {})

Fired when the hole punching protocol completes and both peers can reach each other.

puncher.on('reachable', () => {})

Called when the other peer was able to reach this peer. No guarantee yet that bidirectional communication can be established.

puncher.on('timeout', () => {})

Fired when the hole punching protocol timeouts.

puncher.on('error', (error) => {})

Fired when a fatal error occurs.

Chrome and cordova apps

gulp browserify [--production]

Puts udp-hole-puncher.debug.js and udp-hole-puncher.min.js in build folder. Can be used in chrome and cordova app. When integrating udp-hole-puncher in a cordova app, use cordova-plugin-chrome-apps-sockets-udp:

cordova plugin add https://github.com/MobileChromeApps/cordova-plugin-chrome-apps-sockets-udp

Examples

See examples directory. Note that both peers should not be located behind the same NAT device. To test this lib, deploy one peer on your home network and another one outside of that network -- for instance on a public cloud infrastructure.

To run this test example, execute the following cmd on two machines A and B:

server-A$ npm run-script peer -- --bind=12345 --addr=<PUBLIC ADDR OF B> --port=23456
server-B$ npm run-script peer -- --bind=23456 --addr=<PUBLIC ADDR OF A> --port=12345