Skip to content
Last updated

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.


Log and review event formats

Because event documentation may be limited, log all incoming events to understand their structure and determine which events apply in your application.

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.