Skip to content

buehren/websocket-client-dart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

WebSocket Client in Dart

This simple WebSocket client connects to a given WebSocket URL and sends three texts with a delay of a few seconds. Received data is printed. (Requires Dart.)

Client

> dart pub get

# echo.websocket.org is used by default:
> dart run wsclient.dart

Quarkus Server

The Dart client works with WebSocket echo server running on localhost:

> dart run wsclient.dart --url ws://localhost:8080/websocket

2021-02-26 21:25:22.293451 Starting connection attempt to ws://localhost:8080/websocket ...
2021-02-26 21:25:22.418794 WebSocket readyState: 1
2021-02-26 21:25:23.446877 Sending 'hello'
2021-02-26 21:25:23.458083 Received data: echo of hello
2021-02-26 21:25:24.422013 Sending 'how are you?'
2021-02-26 21:25:24.422752 Received data: echo of how are you
2021-02-26 21:25:26.433450 Sending 'still there?'
2021-02-26 21:25:26.436280 Received data: echo of still there?
^C
^C

The Dart client does NOT work with WebSocket echo server running on Google Cloud Run — the WebSocket is closed right after the Dart client sends data:

> dart run wsclient.dart --url wss://websocket-echo-server-quarkus-xxxxxxxxxx-xx.x.run.app/websocket

2021-02-26 21:25:40.539316 Starting connection attempt to wss://websocket-echo-server-quarkus-xxxxxxxxxx-xx.x.run.app/websocket ...
2021-02-26 21:25:40.844298 WebSocket readyState: 1
2021-02-26 21:25:41.851590 Sending 'hello'
2021-02-26 21:25:41.898587 CONNECTION DONE!
readyState=3
closeCode=1005
closeReason=

2021-02-26 21:25:42.847345 Sending 'how are you?'
2021-02-26 21:25:44.848959 Sending 'still there?'

But this Python client works with WebSocket echo server running on Google Cloud Run:

> python3 wsdump.py wss://websocket-echo-server-quarkus-xxxxxxxxxx-xx.x.run.app/websocket

Press Ctrl+C to quit
> The quick brown fox
< echo of The quick brown fox
> jumps over the lazy cat.
< echo of jumps over the lazy cat.
>

Python Server

The Dart client works with WebSocket echo server running on localhost:

> dart run wsclient.dart --url ws://localhost:8080/websocket

2021-02-26 22:43:12.531323 Starting connection attempt to ws://localhost:8080/websocket ...
2021-02-26 22:43:12.673670 WebSocket readyState: 1
2021-02-26 22:43:13.697287 Sending 'hello'
2021-02-26 22:43:13.712441 Received data: hello
2021-02-26 22:43:14.676114 Sending 'how are you?'
2021-02-26 22:43:14.678580 Received data: how are you
2021-02-26 22:43:16.676518 Sending 'still there?'
2021-02-26 22:43:16.678663 Received data: still there?
^C

The Dart client does NOT work with WebSocket echo server running on Google Cloud Run — the WebSocket is closed right after the Dart client sends data:

> dart run wsclient.dart --url wss://websocket-echo-server-python-xxxxxxxxxx-xx.x.run.app/websocket

2021-02-26 22:43:52.298434 Starting connection attempt to wss://websocket-echo-server-python-xxxxxxxxxx-xx.x.run.app/websocket ...
2021-02-26 22:43:52.619030 WebSocket readyState: 1
2021-02-26 22:43:53.623518 Sending 'hello'
2021-02-26 22:43:53.683049 CONNECTION DONE!
readyState=3
closeCode=1005
closeReason=

2021-02-26 22:43:54.621043 Sending 'how are you?'
2021-02-26 22:43:56.621170 Sending 'still there?'

But this Python client works with WebSocket echo server running on Google Cloud Run:

> python3 wsdump.py wss://websocket-echo-server-python-xxxxxxxxxx-xx.x.run.app/websocket

Press Ctrl+C to quit
> The quick brown fox
< The quick brown fox
> jumps over the lazy cat.
< jumps over the lazy cat.
>

Issue: WebSocket fails with Server on Google Cloud Run + Dart client

Posted in Dart Bug Tracker:

Problem you have encountered

We are currently evaluating Google Cloud Run and Flutter. The project requires a WebSocket connection from a Flutter App to a server container running in Google Cloud Run.

But the combination of a Dart client + Cloud Run seems to be incompatible regarding WebSockets:

  • The WebSocket connection is established, and the Flutter/Dart client receives messages sent by the server on Cloud Run.

  • As soon as the Dart client sends a message to the server, the connection is dropped.

It happens with both a Java/Quarkus and a Python server in Cloud Run. \ It does not happen with those servers on localhost in Docker.

It happens both in a Flutter App running on an Android device and in a Dart program executed from the command line. \ It does not happen with a Python client connecting the same servers.

So the only combination that fails is the one that we need: Dart + Cloud Run.

What you expected to happen

I expect the WebSocket connection to remain intact if the Dart client sends messages over it.

Steps to reproduce

I have created a complete bug reproducer with clients in Dart and Python and servers in Java and Python:

Other information (workarounds you have tried, documentation consulted, etc):

I could not find helpful information in the logs of Google Cloud Run.

I could not find any hint by debugging the Dart client.

Versions, OSs

  • Dart SDK Version (dart --version)
    Dart SDK version: 2.10.5 (stable) (Unknown timestamp) on "linux_x64"
    Dart SDK version: 2.10.5 (stable) (Tue Jan 19 13:05:37 2021 +0100) on "linux_x64"
    Dart SDK version: 2.12.0-259.12.beta (beta) (Fri Feb 12 11:00:34 2021 +0100) on "windows_x64"

  • Whether you are using Windows, MacOSX, or Linux (if applicable)
    Windows, Linux, Android

  • Whether you are using Chrome, Safari, Firefox, Edge (if applicable)
    not applicable

Question

Question posted on StackOverflow:

did anyone use WebSocket connections between a Flutter/Dart client and a server running on Google Cloud Run successfully?

Exactly this combination fails for me.

The client works with the same server on localhost.
Another client works with the same server on Cloud Run.

I tried to find the cause of the problem, so I have created a complete bug reproducer with clients in Dart and Python and servers in Java and Python:

Do you have any idea how this can happen? Did I do anything wrong in my code?

Thank you very much

Thomas

Tags: google-cloud-platform google-cloud-run dart dart-io flutter

About

WebSocket Client in Dart

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages