5

There appears to be 2 ways to maintain an open connection between a Dart server and a Dart client: ServerSocket and Websocket.

https://www.dartlang.org/dart-by-example/#sockets
https://www.dartlang.org/dart-by-example/#websockets

When is it best to use one instead of the other?

OCDev
  • 2,280
  • 3
  • 26
  • 37

1 Answers1

5

Websocket is a protocol build on top normal sockets that are based on the TCP protocol (ServerSocket and Socket). Websockets give you more comfort during programing, because it helps you with:

  • Framing: TCP is stream based, Websockets allow you to send packages. You don't have to find the start and end of your package on your own.
  • Closing Handshake: You can send a connection close reason.
  • Security (in browser context, not required in console application context)
  • You can also access your Websocket server via a Webbrowser API.

If you want to work together with existing Servers / Clients that are using TCP, that you have to use ServerSockets. Websockets and ServerSockets are not compatible (intentionally, for security reasons). As Websockets have more internal stuff to do the performance and throughput will not be as good as raw TCP, but this point is negligible.

Both protocols can be used with encryption, Websockets by using a HTTPS connection (wss://) and TCP using TLS (SecureSocket and SecureServerSocket).

For more details about Websockets, take a look at the RFC. RawDatagramSocket allows you to use the UDP protocol in addition to the TCP based ServerSockets.

Community
  • 1
  • 1
Fox32
  • 13,126
  • 9
  • 50
  • 71