While trying to send a WebSocket transaction, I hit the following error
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.
I am a dedicated quality assurance professional with a true passion for ensuring product quality and driving efficient testing processes. Throughout my career, I have gained extensive expertise in various testing domains, showcasing my versatility in testing diverse applications such as CRM, Web, Mobile, Database, and Machine Learning-based applications. What sets me apart is my ability to develop robust test scripts, ensure comprehensive test coverage, and efficiently report defects. With experience in managing teams and leading testing-related activities, I foster collaboration and drive efficiency within projects. Proficient in tools like Selenium, Appium, Mechanize, Requests, Postman, Runscope, Gatling, Locust, Jenkins, CircleCI, Docker, and Grafana, I stay up-to-date with the latest advancements in the field to deliver exceptional software products. Outside of work, I find joy and inspiration in sports, maintaining a balanced lifestyle.
Thanks. Saved my day
Thanks a LOT
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)
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
Hey,
I am using python websocket client library for getting the notification.
below is the link for the library
https://pypi.org/project/websocket_client/
i am using the short-lived connection and on receive i am getting connection already closed error
Hi,
Did you try out the solution mentioned in the Blog post to have the data in JSON format?
hey,
In my code implementation, I am using data in JSON format, still, the connection is closed.
Ok, can you confirm if you are listening to the correct port and also whether the remote host itself closes the connection
Thank you for this – it helped me get out of a rut
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
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?