6

This is my first time writing a locustfile, and I am trying to implement websocket load testing with locust.

I have basically copied and pasted this example code from the locust plugin, https://github.com/SvenskaSpel/locust-plugins/blob/5abac5852b0e09623bcdd90889d083df3288fead/examples/socketio_ex.py

but it raises a WebsocketConnectionClosedException when 2(ping) is sent.(no response when 2 is sent)

Also, I am creating a ws connection with a real server, and when I debug received messages, it successsully prints socketid. However, no connection logs are printed on the server-side. I am confused here. Is it even making a connection? if not, how can it creates socket id?

Another question is about the event listener. My original client is written with socket.io and I could listen on specific events with the code below. Does python support such feature? How to implement event listener in python?

socket.on("message", (data) => console.log(data))

This is my locustfile

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

    self.connect("wss://<server_url>/socket.io/?EIO=4&transport=websocket", [])

    # example of subscribe
    self.send('42["subscribe",{"url":"/namespace","sendInitialUpdate": true}]')
    print('receive: ' + self.ws.recv())

    # wait until I get a push message to on_message
    while not self.my_value:
        time.sleep(0.1)

    # wait for additional pushes, while occasionally sending heartbeats, like a real client would
    self.sleep_with_heartbeat(10)

def on_message(self, message):
    print('message: ' + message)
    self.my_value = message

if __name__ == "__main__":
    host = "<client_host_url>"

exception message is below

raise WebSocketConnectionClosedException(
websocket._exceptions.WebSocketConnectionClosedException: Connection to remote host was lost.

My client uses react, socket.io-client and server uses nestjs with ws-gateway(socket.io)

Any help is appreciated.

1 Answer 1

2

You’re not supposed to call self.ws.recv() directly. Any messages received will generate a call to on_message(), handle the message there.

Also, what are you trying to do with the extra [] parameter to connect()?

SocketIOUser is a little (very) rough around the edges. You should read the source code for more hints.

Edif: In case someone finds this in 2025, there's now a built in SocketIOUser in Locust core, no need for locust-plugins: https://docs.locust.io/en/stable/api.html#socketio

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer! I am calling self.ws.recv() only to debug received messages, and removing that piece of code doesn't solve the problem. the second parameter of connect() is header and I put empty array to not specify header. Do you know if specifying header array is necessary in my case?
Aha. I dont know about the headers (check what your browser does), I think that depends on your implementation. If your message takes a long time to appear, you should use self.sleep_with_heartbeat() instead of just time.sleep() to make sure to keep the connection alive (you can get the exception you mentioned if the server things the connection has been idle too long)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.