3

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.
Tom3652
  • 2,540
  • 3
  • 19
  • 45

2 Answers2

3

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"}));
nicover
  • 2,213
  • 10
  • 24
0

When the data is received the connection is closed. Just reconnect the server onDone and/or onError.

Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30
  • Thanks for your answer. However i am not receiving any data actually... It seems according to your answer that i had not understood how the Websocket was working, that it needs to reconnect after each event and has no keep alive... Is it this package that require doing such thing ? – Tom3652 Oct 21 '21 at 13:34
  • I have updated the question with the close code status if this can help improve your answer. For now, unfortunately it keeps closing if i try to connect again, this doesn't fix this issue. – Tom3652 Oct 21 '21 at 13:46