Reputation: 45
I was looking out for a sample WebSocket implementation on DartPad.
Here's a link to the implementation: WebSocket Sample
However, while trying to implement the same on my local machine, I encountered that Dart has two implementations of the WebSocket
class.
One is provided in the dart:io
package and another is provided in dart:html
package.
Can anyone provide what's the difference between the two and what are the advantages of one over the other?
Upvotes: 0
Views: 1636
Reputation: 3193
There is no major difference other than some implementation changes in both libraries:
WS in dart:io: https://api.dart.dev/stable/2.7.0/dart-io/WebSocket-class.html
WS in dart:html: https://api.dart.dev/stable/2.7.0/dart-html/WebSocket-class.html#constructors
if you see closely both the classes have the same approach in everything except creating a web socket instance.
dart:io :
Simple no arg constructor is deprecated. But has a factory method to create an instance using a Socket
.
dart:html :
Websocket constructor takes a url
string and a list of protocol
objects.
Upvotes: 1
Reputation: 2007
The one in dart:io is used for standalone app running on Dart VM including Flutter, while the one in dart:html is for Web browsers.
Upvotes: 3