flutter_web_socket 1.0.5 flutter_web_socket: ^1.0.5 copied to clipboard
Flutter WebSocket Utils simplifies WebSocket communication in Flutter apps. Effortlessly integrate real-time data exchange and communication features.
import 'package:flutter_web_socket/flutter_web_socket.dart';
void main() {
connectToWebSocket(
socketUrl: 'wss://socketsbay.com/wss/v2/1/demo/',
).then((webSocket) {
if (webSocket != null) {
// WebSocket connection successful, you can now interact with the server
webSocket.listen(
(data) {
print('Received message: $data');
},
onError: (error) {
print('Error occurred: $error');
},
onDone: () {
print('WebSocket connection closed');
},
);
webSocket.add('Hello, Server!');
// To close the WebSocket connection, you can use:
// webSocket.close();
} else {
// WebSocket connection failed
print('WebSocket connection failed.');
}
});
}