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?
Both WebSocket vs Socket.io are popular choices in the market; let us discuss some of the major Difference Between WebSocket vs Socket.io: It provides the Connection over TCP, while Socket.io is a library to abstract the WebSocket connections. WebSocket doesn't have fallback options, while Socket.io supports fallback.
SSE is best used when it's not necessary to send data from client to server. For example, in status updates and push notification applications, the data flow is from the server to the client only. This is what SSE is designed for, so WebSocket would be overkill.
WebSockets are Full-Duplex meaning both the client and the server can send and receive messages across the channel. Long Polling is Half-Duplex meaning that a new request-response cycle is required each time the client wants to communicate something to the server.
WebSocket is ideal for a scenario where high loads are a part of the game, i.e. real-time scalable chat application, whereas REST is better fitted for occasional communication in a typical GET request scenario to call RESTful APIs.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With