← Load Test Directory

Testing WebSockets with SocketIO

Comprehensive guidance on using LoadForge to perform load tests on WebSocket and SocketIO based applications.

You are now browsing the LoadForge locust test directory. You can use these tests as a starting point for your own tests, or use our AI wizard to generate one automatically.

World

Overview

LoadForge provides dedicated support for WebSocket testing via SocketIO. While there's a general guide for testing standard WebSockets which can be found in our default websockets test guide, the SocketIO implementation offers a streamlined approach for testing. Below, we outline the process and provide a sample script to help you get started. WebSockets offer a full-duplex communication channel over a single, long-lived connection. It's a key technology for real-time applications, but load testing them can be tricky. SocketIO, a library that abstracts WebSocket connections, makes the testing process easier. With LoadForge's support for SocketIO, users can conveniently simulate large numbers of WebSocket connections to test the performance of their real-time applications.

Sample WebSocket Test with SocketIO

The following Python script provides a basic template for a SocketIO-based WebSocket load test:

import time
import json
from locust import task
from locust_plugins.users import SocketIOUser

class MySocketIOUser(SocketIOUser):
    @task
    def my_task(self):
        self.my_value = None

        # Connect to your WebSocket endpoint
        self.connect("wss://YOUR_WS_URL_HERE/socket.io/?EIO=3&transport=websocket")

        # An example to demonstrate subscribing to a specific channel or topic
        self.send('42["subscribe",{"url":"/game/237382","sendInitialUpdate": true}]')

        # Wait until a push message is received on `on_message`
        while not self.my_value:
            time.sleep(0.1)

        # HTTP requests can also be performed within the same task
        self.client.get("/")

        # Simulating a real client by waiting for pushes and occasionally sending heartbeats
        self.sleep_with_heartbeat(10)

    def on_message(self, message):
        # Parsing and processing the received message
        self.my_value = json.loads(message)["my_value"]

    if __name__ == "__main__":
        host = "http://example.com"

Ensure you replace YOUR_WS_URL_HERE with the appropriate WebSocket URL for your application.


Locust Test Example

LoadForge leverages the power of the open-source tool, Locust. This means that users of the open-source version of Locust can directly use the above script. If you're already familiar with Locust, you can easily enhance your testing capabilities by importing your script into LoadForge.

Conclusion

Load testing WebSocket applications, especially those built with SocketIO, is simplified with LoadForge's support. Whether you're a seasoned tester or just getting started, the above guide and script provide a solid foundation for your WebSocket testing needs.

Ready to run your test?
Launch your locust test at scale.