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.


11 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 org.eclipse.jetty.websocket.client.WebSocketClient@35c6f96b 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

      1. hey,
        In my code implementation, I am using data in JSON format, still, the connection is closed.

  2. I am sending 5 req to the server, easily getting all the responses, but when I am sending 12 req and expecting 12 responses, suddenly after receiving 6 responses, getting log as WebSocket connection is closed

    1. Hi,
      As per Websocket docs, 12 requests is within the limit of requests the Websocket can handle at a time. The connection would get closed only if the connection handler exits. So adding debug statements to the loop that processes the request JSON messages would help gain some insights. Also you may need to check on the size of the request messages?

Leave a Reply

Your email address will not be published. Required fields are marked *