WebSocket Error – Connection is already closed.

While trying to send a WebSocket transaction, I hit the following error

Connection is already closed.

I received this error when working on an application that involved a WebSocket connection. The Websocket handshake between the client and server was successful. After that I was able to send data to server using ws.send(“request_data_1”). I had no trouble receiving the data using ws.recv(). But when I sent the next frame of data using ws.send(“request_data_2”), I got the Connection is already closed and socket is already closed error message. I couldn’t figure out why exactly the connection was getting closed.


After some research, I figured out that the server was expecting data in JSON format. I transformed the data being sent to json format and things started working smoothly.

ws.send(json.dumps("request_data_1"))

When looking up this error online, I noticed Google was not very helpful. So I thought I would write this short post to help anyone else who stumbled across this error. Hope this helped!

If you liked this article, learn more about Qxf2’s testing services for startups.


6 thoughts on “WebSocket Error – Connection is already closed.

  1. My Requirement:

    I have to connect to the Websocket through JMeter Once it is connected, I have to send some API requests having JSON data as body to the server and I have to verify the JSON response of it

    Worked Items:

    I have tried using Maciej Zaleski Websocket plugin but couldnt make through I am able to connect to the websocket using Websocket open Connection but after that I am unable to send JSON data to the server Throwing an error as

    “Error: [Execution Flow] – Opening new connection – Using response message pattern “” – Using disconnect pattern “” – Waiting for the server connection for 5000 MILLISECONDS – Cannot connect to the remote server

    [Variables] – Message count: 0

    [Problems] – Unexpected error: null JMeter.plugins.functional.samplers.websocket.ServiceSocket.sendMessage(ServiceSocket.java:189) JMeter.plugins.functional.samplers.websocket.WebSocketSampler.sample(WebSocketSampler.java:141) org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490) org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) java.lang.Thread.run(Unknown Source)

    Log: 2018-08-07 14:57:54,013 INFO o.e.j.w.c.WebSocketClient: Stopped [email protected] 2018-08-07 14:57:54,014 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, local)

    1. Hi ,
      You can refer these links below
      i)https://www.blazemeter.com/blog/jmeter-websocket-samplers-a-practical-guide
      ii)https://bitbucket.org/pjtr/jmeter-websocket-samplers

Leave a Reply

Your email address will not be published.