Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep WebSocket connection Alive - Flutter AWS?

I am trying to connect to a Websocket API hosted in AWS from my Flutter app.
I am using the web_socket_channel package to build a real-time tchat app.

I have different routes for my WebSocket API : $connect, $disconnect and sendMessage. I would like to send events to the server and get responses in dart.

So far, i have no way to debug this as the web_socket_channel doesn't offer this possibility... So i am simply not receiving events nor sending them (no logs in my CloudWatch log group, whereas i have some using wscat or Postman tool that are both working fine).

Here is my code :

print("Connecting to websocket...");
    try {
      IOWebSocketChannel channel = IOWebSocketChannel.connect(
        Uri.parse('wss://my_websocket_endpoint'),
      );
      print("Protocol : ${channel.protocol}");
      channel.stream.listen((message) {
        print("Message is : $message");
        //channel.sink.add('received!');
        //channel.sink.close(goingAway);
      },
        onDone: () {
          print("Disconnected, done.");
          print("Close reason : ${channel.closeReason}");
          print("Close code : ${channel.closeCode}");
        },
        onError: (error) {
          print("Error listening : $error");
        },
      );
      channel.sink.add({"action": "sendMessage", "data": "test"});
    }
    catch (error) {
      print("Error connecting : $error");
    }

After looking and cleaning up the logs, i have realized that the above code was calling the $connect route and 100ms later the $disconnect route.

So a lead to an answer would be : Why the connection is not kept alive ? (i am not closing anything in dart, this is my only piece of code dealing with the socket)

EDIT :

As mentioned in this answer, i have added the onDone and the onError callbacks to my code that is called immediately after the connection.

onError is never called.

Why so ? When other tools stay connected ?

EDIT 2 :

I am adding here the logs of the connection in API Gateway :

(clientID=) Client [Connection Id: clientID=] disconnected from API [apiID] with integration response status code [200]. Close reason: [1006: Connection closed abnormally]

According to the website :

LWS_CLOSE_STATUS_ABNORMAL_CLOSE     
1006 is a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. It is designated for use in applications expecting a status code to indicate that the connection was closed abnormally, e.g., without sending or receiving a Close control frame.

And client side i have caught the status code 1005:

LWS_CLOSE_STATUS_NO_STATUS  
1005 is a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. It is designated for use in applications expecting a status code to indicate that no status code was actually present.
like image 975
Tom3652 Avatar asked Mar 23 '26 07:03

Tom3652


1 Answers

You need to “Stringify” your data before passing it to the request.
The body should be a string so you can use :

channel.sink.add(jsonEncode({"action": "sendMessage", "data": "test"}));
like image 113
nicover Avatar answered Mar 25 '26 22:03

nicover



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!