labthings_fastapi.websockets

Handle notification of events, property, and action status changes.

There are several kinds of “event” in the WoT vocabulary, not all of which are called Event, which is why this module is called notifications. In all cases, these are events that happen on an exposed Thing, and may need to be relayed to one or more listeners (currently via a WebSocket connection, though long polling may also be an option in the future).

The aim at this stage (July 2023) is for a minimal working example that enables property changes to be fed via a websocket. Events proper should not be a big step thereafter.

The W3C standard does not define a way for one websocket to handle multiple Things, so for now the websocket endpoint will be associated with a single Thing instance. This may change in the future.

  1. Richard Bowman July 2023, released under GNU-LGPL-3.0

Functions

relay_notifications_to_websocket(→ None)

Relay objects from a stream to a websocket as JSON.

process_messages_from_websocket(→ None)

Process messages received from a websocket.

websocket_endpoint(→ None)

Handle communication to a client via websocket.

Module Contents

async labthings_fastapi.websockets.relay_notifications_to_websocket(websocket: fastapi.WebSocket, receive_stream: anyio.abc.ObjectReceiveStream) None

Relay objects from a stream to a websocket as JSON.

Interaction Affordances (events, actions) that we’ve registered with will post messages to the queue: this function takes those messages from the queue and passes them to the websocket.

Parameters:
  • websocket – the WebSocket we are communicating over.

  • receive_stream – an anyio.abc.ObjectReceiveStream that will yield objects that we send over the websocket.

async labthings_fastapi.websockets.process_messages_from_websocket(websocket: fastapi.WebSocket, send_stream: anyio.abc.ObjectSendStream, thing: labthings_fastapi.thing.Thing) None

Process messages received from a websocket.

Currently, this will allow us to observe properties, by registering (or de-registering) for those properties.

Parameters:
async labthings_fastapi.websockets.websocket_endpoint(thing: labthings_fastapi.thing.Thing, websocket: fastapi.WebSocket) None

Handle communication to a client via websocket.

This function handles a websocket connection to a Thing‘s websocket endpoint. It can add observers to properties and actions, and will forward notifications from the property or action back to the websocket.

Parameters:
  • thing – the Thing the websocket is attached to.

  • websocket – the web socket that has been created.