WebSockets with SocketIO

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

LoadForge can record your browser, graphically build tests, scan your site with a wizard and more. Sign up now to run your first test.

Sign up now


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. This guide outlines the process and provides sample scripts for SocketIO testing. 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"]

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.

Locust Test Script (locust.py)

# locust.py
import time
import json
from locust import between, task
from locust_plugins.users import SocketIOUser

class MySocketIOUser(SocketIOUser):
    wait_time = between(1, 3)

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

    @task
    def my_task(self):
        # Subscribe
        self.send('42["subscribe",{"url":"/game/237382","sendInitialUpdate": true}]')
        # Maintain connection
        self.sleep_with_heartbeat(10)

    def on_message(self, message):
        # Process incoming message
        _ = json.loads(message)

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.