WebSockets on Multistep checks
To create a WebSocket check in Checkly, you’ll need to write a script that establishes a WebSocket connection, sends messages, and validates responses.
Set Up the WebSocket Connection
First, we’ll establish a connection to the WebSocket server. Replace wss://your.websocketserver.com
with the URL of your WebSocket server.
Handle WebSocket Events
Handle the key events like opening the connection, receiving messages, and encountering errors.
Sending and Receiving Messages
Implement the logic for sending messages and processing the received responses.
Close the Connection
Ensure to properly close the WebSocket connection after your checks.
Example: Monitoring a WebSocket Connection
Here’s the complete example script that demonstrates a WebSocket check. It sends a subscription message, prints out the echo’d response, handles different types of incoming messages and closes the connection after 5 seconds. We’ve also added an object assertion to check the data returned from the WebSocket is correct. If the message does not match, the script will throw an error, indicating a discrepancy in the expected data.
Don’t forget the essentials:
- Error Handling: Implement robust error handling for scenarios like connection failures and timeouts.
- Message Validation: Add logic to validate incoming messages to ensure data integrity.
- Closing Connections: Always close the WebSocket connection gracefully to avoid resource leaks.
Last updated on December 13, 2024. You can contribute to this documentation by editing this page on Github