Mark Ireland
Mark Ireland

Reputation: 175

Is there a case for Websocket clients to close the connection?

Please excuse any ignorance or terminology failures on my part here. I work for a software company, but in support, not development.

We have developed a tool that connects to a websocket server to pass and receive messages. For receiving messages, it opens a connection and receives messages (passing them on to other components for further processing) until the server closes the connection.

With the simple Websocket server I tested it against (ws://echo.websocket.org:80) I used our tools to ping a message to the server and receive it back. But the server didn't close the connection and our tool just kept on listening, when I would have expected it to stop.

I suggested to our dev team that we might want to add a setting for the client to close the connection after X messages received, or Y length of time, but they have challenged whether this is realistic.

So, is it intended that a Websocket client should be able to close the connection and is it bad practice from the development point of view? Can a client even tell if a message has completed? And if a client can close a connection, what sort of scenarios might require this?

Upvotes: 2

Views: 3537

Answers (1)

leggetter
leggetter

Reputation: 15467

So, is it intended that a Websocket client should be able to close the connection

The WebSocket API contains a close function. This is there so that the client can close the connection to the server.

is it bad practice from the development point of view? And if a client can close a connection, what sort of scenarios might require this?

It really depends on the application. But - as above - it's part of the WebSocket API so it's entirely valid to do so.

Can a client even tell if a message has completed?

WebSocket.onmessage is called when a message is received. Whether the entire message has been received really depends from your application's point of view. If your server sends a full message in a single go then it is very likely that a full message will be received. You'll find exact details in the WebSocket protocol documentation

Upvotes: 3

Related Questions