https://github.com/hamed-rezaee/dart_web_socket_handler
https://github.com/hamed-rezaee/dart_web_socket_handler
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/hamed-rezaee/dart_web_socket_handler
- Owner: hamed-rezaee
- Created: 2023-08-02T01:38:04.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-04T08:56:31.000Z (about 2 years ago)
- Last Synced: 2025-01-26T12:25:04.790Z (9 months ago)
- Language: Dart
- Size: 29.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# WebSocket Connection Handler
This is a WebSocket connection handler package that simplifies the management of WebSocket connections and provides a high-level API for handling WebSocket communication in Dart applications. The package is built on top of the `web_socket_channel` library but abstracts away some of the complexities of directly using `web_socket_channel`.
### Example
Here's a simple example of how to use the WebSocket connection handler:
```dart
import 'dart:async';
import 'dart:convert';
import 'dart:io';import 'package:dart_web_socket_handler/web_socket_handler.dart';
void main() async {
// Define the [uri] to which to connect.
final Uri uri = Uri.parse('wss://ws.binaryws.com/websockets/v3?app_id=1089');
// Create a [WebSocket] instance from [uri] and connect to it.
final WebSocket socket = WebSocket(uri);// Listen to the [connection] state changes.
socket.connection.listen(stdout.writeln);
// Listen to the [messages] received from the server.
socket.messages.listen(stdout.writeln);// Send a message to the server.
socket.send(jsonEncode({'ticks': 'R_50', 'subscribe': 1}));// Wait for 60 seconds to receive messages from the server.
// This is just for preventing the program from exiting.
await Future.delayed(const Duration(seconds: 60));// Close the connection.
await socket.close();
}
```### Benefits
- `Simplified API`: This package abstracts away the low-level details of WebSocket management and provides a higher-level API for ease of use.
- `Connection State Tracking`: The ConnectionController allows you to easily track the state of the WebSocket connection and handle state changes accordingly.
- `Automatic Reconnections`: The WebSocket class handles automatic reconnections in case of disconnection, ensuring a more robust WebSocket experience.
- `Web and IO Support`: The package provides implementations for both web and IO platforms.
- `Queue Requests`: The WebSocket class queues requests while the WebSocket connection is not open and sends them once the connection is open.### Issue Solved
The main issue this package solves is the complexity of directly using `web_socket_channel` and handling WebSocket connections manually. By providing a higher-level abstraction and state management, this package makes it easier to work with WebSockets, simplifies error handling and reconnects, and improves overall code organization.
### Credits
This package is inspired by [Felix Angelov's web_socket_client package](https://github.com/felangel/web_socket_client) and customized to fit project requirements.