Fundamentals 19 min read

An Overview of W3C Web of Things (WoT) Standards, APIs, and Discovery Mechanisms

This article introduces the W3C Web of Things (WoT) standards, explains the WoT Architecture, Thing Description, and Scripting API, describes supported use‑cases, details the ConsumedThing, ExposedThing, and ThingDiscovery interfaces, and provides multiple JavaScript code examples illustrating how to fetch TDs, create things, read/write properties, invoke actions, and perform discovery via local, directory, and multicast methods.

360 Tech Engineering
360 Tech Engineering
360 Tech Engineering
An Overview of W3C Web of Things (WoT) Standards, APIs, and Discovery Mechanisms

The author, a senior web‑frontend expert at 360, presents a concise guide to the W3C Web of Things (WoT) standards, focusing on the CR‑stage WoT Architecture and Thing Description as well as the WD‑stage WoT Scripting API.

WoT Scripting API enables scripts to expose and consume Things. A client creates a ConsumedThing by fetching a Thing Description (TD) and can read, write, observe properties, invoke actions, and subscribe to events. A server creates an ExposedThing to expose properties, actions, and events, optionally providing custom handlers.

The article lists the supported scenarios (consuming, exposing, discovering Things) and defines the key interfaces:

ThingDescription – the JSON schema describing a Thing.

WOT – provides consume(td) , produce(td) , and discover(filter) methods.

ConsumedThing – client‑side API with methods such as readProperty(name) , writeProperty(name, value) , invokeAction(name, params) , observeProperty(name, listener) , and subscribeEvent(name, listener) .

ExposedThing – server‑side API extending ConsumedThing with setPropertyReadHandler , setPropertyWriteHandler , setActionHandler , emitEvent , expose() , and destroy() .

ThingDiscovery – discovery API with start() , next() , stop() , and attributes filter , active , done , error . It uses a ThingFilter dictionary to specify discovery method (any, local, directory, multicast) and optional URL, query, or fragment.

Key code examples are provided:

Example 1 – Fetch TD and create ConsumedThing

try { let res = await fetch('https://tds.mythings.biz/sensor11'); let td = await res.json(); let thing = new ConsumedThing(td); console.log('Thing name: ' + thing.getThingDescription().title + ' consumed.'); } catch (err) { console.log('Fetching TD failed', err.message); }

Example 2 – Client API usage

try { let res = await fetch('https://tds.mythings.org/sensor11'); let td = await res.json(); let thing = new ConsumedThing(td); console.log('Thing ' + thing.getThingDescription().title + ' consumed.'); await thing.observeProperty('temperature', value => { console.log('Temperature changed to: ' + value); }); await thing.subscribeEvent('ready', data => { console.log('Ready; index: ' + data); await thing.invokeAction('startMeasurement', { units: 'Celsius' }); console.log('Measurement started.'); }); } catch (e) { console.log('Error:', e.message); }

Example 3 – Create ExposedThing with a simple property

let temperaturePropertyDefinition = { type: 'number', minimum: -50, maximum: 10000 }; let tdFragment = { properties: { temperature: temperaturePropertyDefinition }, actions: { reset: { description: 'Reset the temperature sensor', input: { temperature: temperatureValueDefinition }, output: null, forms: [] } }, events: { onchange: temperatureValueDefinition } }; let thing1 = await WOT.produce(tdFragment); await thing1.writeProperty('temperature', 0); thing1.setPropertyReadHandler('temperature', () => readLocalTemperatureSensor()); await thing1.expose();

Example 4 – Add or modify properties on an existing ExposedThing

let instance = JSON.parse(JSON.stringify(thing1.td)); instance.name = 'mySensor'; instance.properties.brightness = { type: 'number', minimum: 0.0, maximum: 100.0, required: true }; instance.properties.status = statusValueDefinition; instance.actions.getStatus = { description: 'Get status object', input: null, output: { status: statusValueDefinition }, forms: [] }; instance.events.onstatuschange = statusValueDefinition; let thing2 = new ExposedThing(instance); await thing2.expose();

Example 5 – Discover local Things

let discovery = new ThingDiscovery({ method: 'local' }); do { let td = await discovery.next(); console.log('Found Thing Description for ' + td.title); let thing = new ConsumedThing(td); console.log('Thing name: ' + thing.getThingDescription().title); } while (!discovery.done);

Example 6 – Discover Things via a directory with timeout

let discoveryFilter = { method: 'directory', url: 'http://directory.wotservice.org' }; let discovery = new ThingDiscovery(discoveryFilter); setTimeout(() => { discovery.stop(); console.log('Discovery stopped after timeout.'); }, 3000); do { let td = await discovery.next(); console.log('Found Thing Description for ' + td.title); let thing = new ConsumedThing(td); console.log('Thing name: ' + thing.getThingDescription().title); } while (!discovery.done); if (discovery.error) { console.log('Discovery stopped because of an error: ' + discovery.error.message); }

Example 7 – Open‑ended multicast discovery with timeout

let discovery = new ThingDiscovery({ method: 'multicast' }); setTimeout(() => { discovery.stop(); console.log('Stopped open‑ended discovery'); }, 10000); do { let td = await discovery.next(); let thing = new ConsumedThing(td); console.log('Thing name: ' + thing.getThingDescription().title); } while (!discovery.done);

The article concludes with further reading links to the official WoT specifications and related resources.

JavaScriptIoTScripting APIThing DescriptionWeb of ThingsWoT
360 Tech Engineering
Written by

360 Tech Engineering

Official tech channel of 360, building the most professional technology aggregation platform for the brand.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.