Skip to content

About

Node.js implementation of a W3C WoT Web Thing

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

webthing-node-2

Node.js implementation of a W3C WoT Web Thing.

This is a work-in-progress pure Node.js replacement for the webthing-node library which will implement the W3C WoT Thing Description, W3C WoT Discovery and (proposed) W3C WoT Thing Protocol specifications.

Example Web Thing

To run an example web thing representing a lamp:

$ npm run example

Create Your Own Web Thing

Import the webthing library:

import { Thing, ThingServer } from 'webthing';

Create a partial Thing Description without Forms or security definitions:

const partialTD = {
  title: 'My Lamp',
  description: 'A web connected lamp',
  properties: {
    on: {
      type: 'boolean',
      title: 'On/Off',
      description: 'Whether the lamp is turned on',
    },
    level: {
      type: 'integer',
      title: 'Brightness',
      description: 'The level of light from 0-100',
      unit: 'percent',
      minimum: 0,
      maximum: 100,
    },
  },
  actions: {
    fade: {
      title: 'Fade',
      description: 'Fade the lamp to a given level',
      synchronous: false,
      input: {
        type: 'object',
        properties: {
          level: {
            title: 'Brightness',
            type: 'integer',
            minimum: 0,
            maximum: 100,
            unit: 'percent',
          },
          duration: {
            title: 'Duration',
            type: 'integer',
            minimum: 0,
            unit: 'milliseconds',
          },
        },
      },
    },
  },
  events: {
    overheated: {
      title: 'Overheated',
      data: {
        type: 'number',
        unit: 'degree celsius',
      },
      description: 'The lamp has exceeded its safe operating temperature',
    },
  },
};

Instantiate a Web Thing from the partial Thing Description:

const thing = new Thing(partialTD);

Properties

Set property read and write handlers for each property defined in the Thing Description:

thing.setPropertyReadHandler('on', async function () {
  return true;
});

thing.setPropertyReadHandler('level', async function () {
  return 50;
});

thing.setPropertyWriteHandler('on', async function (value) {
  // Update the lamp on/off state.
});

thing.setPropertyWriteHandler('level', async function (value) {
  // Update the lamp level.
});

Property values must conform to the data schema defined in the Thing Description.

Property changes are emitted automatically when an observable property is written via its write handler, but for other property changes you should explicitly call emitPropertyChange() to notify active SSE observers:

await thing.emitPropertyChange('on', true);

If the value argument is omitted then the current value is read using the read handler and that value is emitted:

await thing.emitPropertyChange('on');

Actions

Set action invoke handlers for each action defined in the Thing Description:

thing.setActionInvokeHandler('fade', async (input, signal) => {
  // Check signal.aborted or listen for its "abort" event during long-running work.
});

Action inputs and outputs must conform to their respective data schemas defined in the Thing Description.

For synchronous actions set synchronous to true in the Thing Description. The invoke handler should accept the action input as an argument and return a promise which resolves with the action output.

For asynchronous actions set synchronous to false in the Thing Description. The invoke handler should accept the action input as its first argument, and an optional AbortSignal as a second argument so the action can be stopped if the request is cancelled.

Events

Call emitEvent() whenever an event occurs for one of the event affordances defined in the Thing Description, passing the event name and data payload (if any) as arguments:

thing.emitEvent('overheated', 90);

Event payloads must conform to the event data schema defined in the Thing Description.

Thing Server

Expose the Thing to the Web of Things by instantiating a Thing Server for the Thing, then calling start() to start its web server (providing a port number as an argument):

const server = new ThingServer(thing);

server.start(8080);

To serve over HTTPS, provide Node.js HTTPS server options with your certificate and private key. The optional domain replaces the request host in the generated Thing Description:

import { readFileSync } from 'node:fs';

const secureServer = new ThingServer(thing, {
  tls: {
    key: readFileSync('/path/to/private-key.pem'),
    cert: readFileSync('/path/to/certificate.pem'),
  },
  domain: 'thing.example.com',
});

secureServer.start(8443);

The certificate and key must be obtained and renewed by your deployment; do not commit private keys to your project. Without tls options, the server continues to use HTTP.

Development

Type Checking

You can type check this library against JSDoc annotations with:

$ npm run typecheck

Fixing Formatting

$ npm run format (to write the changes)

$ npm run format:check (just to run checks without writing)

Lint

Run the linter with:

$ npm run lint

Running Tests

You can run tests with:

$ npm test

About

Node.js implementation of a W3C WoT Web Thing

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages