The right way to handle websocket

I have a connection to the Websocket server, which should be there for 40 seconds or so. Ideally, this should be forever open.

The client constantly sends data to the server and vice versa.

I am using this sequence now:

var socket; function senddata(data) { if (!socket) { socket = new WebSocket(url); socket.onopen = function (evt) { socket.send(data); socket.onmessage = function (evt) { var obj = JSON.parse(evt.data); port.postMessage(obj); } socket.oneerror = function (evt) { socket.close(); socket = null; } socket.onclose = function(evt){ socket = null; } } } else { socket.send(data); } } 

Obviously, according to the current logic, in case of an error, the current request data may not be sent at all.

To be honest, sometimes an error occurs that the websocket is still in a connected state. This connection often breaks due to network problems. In short, this does not work perfectly.

I read the best design: How to wait for ReadyState for WebSocket to change , but does not cover all the cases that I need to handle.

I also thought about it, but could not get the correct procedure for this.

So, what is the correct way to send regular data through Websockets, which does a good job of problems like disconnection, etc.

+8
javascript websocket
source share
4 answers

An event that you do not seem to close is closed. This should work very well, as it is called whenever the connection is completed. This is more reliable than onerror, because not all connection failures lead to an error.

+2
source share

I personally use Socket.IO , it provides two-way communication based on events between the client and server in real time.

This event is managed. Events such as

on connection :: socket.on('conection',callback);

and

on disconnect :: socket.on('disconnect',callback);

are built into socket.io, so it can help you with connection problems. Quite a lot of easy to use, check out their website if you are interested.

+1
source share

I use a two-layer scheme on the client: abstract-wrapper + websocket-client:

Websocket-client responsibilities interact with the server, reconnect and provide interfaces (event-emitter and some methods) for the abstract shell.

abstract-wrapper is a high-level level that interacts with a websocket client, subscribes to its events and aggregates data when the connection is temporarily down. An abstract shell can provide the application layer with any interface, such as Promise, EventEmitter, etc.

At the application level, I just work with an abstract shell and don't worry about communication or data loss. Undoubtedly, it’s nice to have here information about the status of the connection confirmation and data transfer, because it is useful.

If necessary, I can provide some code, for example

+1
source share

This is apparently a problem with the server, not a problem in the client.

I do not know what the server looks like here. But that was a huge problem for me in the past when I was working on a websocket based project. The connection will continuously break.

So, I created a websocket server in java and solved my problem.

websockets depend on many settings, for example, if you use servlets, and servlet server parameters matter if you use some php, etc., apache and php parameters matter, for example, if you create a web server server in php and php has a default timeout of 30 seconds, it will break after 30 seconds. If keep-alive is not established, the connection will not survive, etc.

What you can do as a quick fix is

  • keep sending messages to the server after a certain time (for example, after 2 or 3 seconds, so if the website is disconnected, it is known to the client, so it can call onclose or ondisconnect, I hope you know there is no way to find if the connection violated, except that he did not send anything.
  • check keep-alive server header
  • If you have access to the server, then these are timeouts, etc.

I think that would help

0
source share

All Articles