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

hapi-coap-listener

v0.4.0

Published

CoAP listener for Hapi

Downloads

11

Readme

hapi-coap-listener Build Status

CoAP listener for Hapi

Install

$ npm install hapi-coap-listener

Requirements

Usage

// app.js

let Hapi = require('hapi');

// add your own defaults here
let server = new Hapi.Server({
  app: {
    coap: {
      host: 'localhost'
    }
  }
});

// connection props generated for you include: uri, listener, autoListen (true),
// and tls (false).  
let options = require('hapi-coap-listener')(server, {
  port: 5693, // this is the port of the CoAP server
  labels: ['coap', 'on-a-rope'] // default label is 'coap'
  sock: null // define a socket path here if you wish; otherwise one is created
});

server.connection(options);

server.route({
  method: 'GET',
  path: '/',
  handler: function(req, reply) {
    reply('Hello world!');
  })
});

server.start(function(err) {
  if (err) {
    throw new Error(err);
  }
  console.log(`Hapi listening on ${server.info.uri}`); 
});

Try it with coap-cli:

$ node /path/to/app.js # start CoAP server

In another shell:

$ npm install -g coap-cli
$ coap get coap://localhost/

The How's and Why's

CoAP, at first glance, is fairly similar to HTTP, with its notion of "options" ("headers"), URL paths and modes. Seems like a great fit for "web server" frameworks, doesn't it?

Well, yes and no.

The main problem arises from the fact that CoAP is bound to UDP instead of TCP. This means it has no notion of a connection. Hapi, and just about any other web server framework you will find, assumes you are listening with an HTTP server for HTTP traffic (if they didn't, they'd suck). So, a web server will listen for the connection event to determine how to handle requests and responses from a client.

Without a connection, you can't run a web server. CoAP has no connections. This looks grim.

But a cool thing about Hapi (and other frameworks as well, but I like Hapi) is that it gives you some wiggle room. You can hand it a generic TCP server (think net.Server()) for a listener (see server.connection()). Even better, it doesn't need to bind to a port of a network interface, and can bind to a UNIX socket (or Windows pipe). Hapi will listen on that TCP server for HTTP requests and reply with HTTP responses--even if it's listening on some file in /tmp/. Furthermore, it streamlines "faking" connections with server.inject().

This module gives Hapi a dummy TCP server acting as a proxy to a CoAP server. Rough flow:

  1. A client requests coap://host:port/some/route
  2. CoAP server injects the request into the TCP server
  3. Hapi dispatches the request and any routes, handlers, etc. are invoked
  4. Upon reply, the callback function CoAPifies* the response object, then issues a proper response to the client

What happened to the UNIX socket? Nothing. We don't use it. Then why not just forget about the TCP server, and inject into a HTTP listener? Loose coupling, mainly--a separate connection allows you to make CoAP- or HTTP-only routes, configuration, or runtime data. Indeed, as-of-yet unimplemented features (see below) may further necessitate the schism. Also, this assumes you're running a HTTP server.

*CoAPification: translating an HTTP request or response into a CoAP request or response, respectively

Roadmap

Currently, this module does not support anything beyond basic requests and responses. So:

  1. Observe mode (multiple responses per request)
  2. Multicast (possible? no idea)
  3. DTLS (probably impossible without monkeypatching Hapi)
  4. Blockwise transfers (I have no idea what this even is)

License

© 2015 Christopher Hiller. Licensed MIT.