Connect to real-time events
The Prep provides real-time updates through WebSocket connections. The events WebSocket allows for monitoring system state changes, protocol execution progress, and other important notifications as they occur.
Connecting to WebSocket events enables your application to respond to system changes in real-time, providing a more efficient alternative to polling the REST API.
Connect to the events WebSocket
To connect to the events WebSocket, establish a WebSocket connection to the following endpoint:
ws://[IP_address]/NimbusLite/instinctevents
Replace [IP_address] with the IP address of the Prep. Go to Access OpenAPI documentation for instructions on finding the IP address.
Understand the event message format
Every message on the events WebSocket uses the same envelope:
{
"code": "maintenancedata",
"description": "Maintenance Data",
"data": "{\"id\":\"5854c6bfedba41cba94560861b0075d8\", ... }"
}The code field identifies the kind of event and is what your application dispatches on.
The description field is a fixed human-readable label for the event type. Do not branch on it.
The data field is the payload of the event.
The data field is a JSON-encoded string, not a nested object. Parse it a second time to reach the payload. Identifiers that other endpoints require, such as the view ID used by the maintenance close-view endpoint, are inside that inner payload and never appear at the top level.
Event codes are emitted in lowercase. Lowercase the code value before comparing it so that your dispatch does not depend on text case.
Review the common event codes
The following event codes are emitted, regardless of what the instrument is doing.
|
Code |
Description |
Payload |
|---|---|---|
|
finished |
The current run or procedure completed and the system is idle. |
Empty string |
|
timeremaining |
Countdown tick for a timed operation. |
runStartTime, runCurrentTime, runEstimatedEndTime, pauseSeconds |
|
generic |
A mirror of every inbound instrument event. |
Name, ClientState, Payload, Source |
A generic event is emitted for every inbound instrument event, so expect high volume. Most applications can ignore it. Unlike the other payloads on this socket, its inner fields use initial capitals:
{
"Name": "statechanged",
"ClientState": "running",
"Payload": "aborted",
"Source": "schedule"
}The Payload field of a generic event is sometimes a bare string, such as aborted or paused, and sometimes a JSON object. Wrap the second parse in a try/catch and fall back to string comparison.
Other codes are specific to what the instrument is doing. The maintenancedata and messagebox codes carry interactive prompts and require a response. Go to Run a maintenance procedure for those payloads and the response loop.
Log and review event formats
The event codes listed in Review the common event codes are the ones most applications need. To discover codes that are specific to an operation you are automating, log all incoming events and review their structure.
To log events:
-
Establish a connection to the events WebSocket endpoint.
-
Set up a message handler that logs all incoming messages to the console or a file.
-
Perform various operations on the Prep (such as starting a protocol, run maintenance, or triggering errors).
-
Review the logged events to identify their format and structure, and the conditions that trigger them.
By logging events during different operations, you can build a comprehensive understanding of the event types available and their payloads.
Example connection code
JavaScript
const ws = new WebSocket('ws://[IP_address]/NimbusLite/instinctevents');
ws.onopen = () => { console.log('Connected to events WebSocket');
};
ws.onmessage = (event) => {
console.log('Event received:', event.data);
const eventData = JSON.parse(event.data);
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
ws.onclose = () => {
console.log('Disconnected from events WebSocket');
};Python
import websocket
import json
def on_message(ws, message):
print(f"Event received: {message}")
event_data = json.loads(message)
def on_error(ws, error):
print(f"WebSocket error: {error}")
def on_close(ws, close_status_code, close_msg):
print("Disconnected from events WebSocket")
def on_open(ws):
print("Connected to events WebSocket")
ws = websocket.WebSocketApp(
"ws://[IP_address]/NimbusLite/instinctevents",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close
)
ws.run_forever()C#
using System;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
var ws = new ClientWebSocket();
await ws.ConnectAsync(new Uri("ws://[IP_address]/NimbusLite/instinctevents"), CancellationToken.None);
Console.WriteLine("Connected to events WebSocket");
var buffer = new byte[1024 * 4];
while (ws.State == WebSocketState.Open)
{
var result = await ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
var message = Encoding.UTF8.GetString(buffer, 0, result.Count);
Console.WriteLine($"Event received: {message}");
}Related WebSockets
In addition to the events WebSocket, the Prep also provides:
-
Errors WebSocket: ws://[IP_address]/NimbusLite/instincterrors - For runtime error notifications. Go to Error handling for more information.
Runtime errors are not delivered on the events WebSocket. Subscribe to both sockets if your application needs to detect and resolve errors while an operation is running.