0 purchases
zaptools client
Zaptools #
A toolkit for Event-Driven websocket management
Getting started #
Zaptools provides tools for building event-driven websocket integration. It is built on top websocket.
Usage #
In order to get a connection with the server, zaptools-dart provides two clients: based on callbacks and based on streams.
ZapConsumer trigger a callback when a event is invoked
final zConsumer = ZapConsumer('ws://127.0.0.1:8000/')..connect();
zConsumer.onConnected((eventData) {
print('connected!');
});
zConsumer.onDisconnected((eventData) {
print('disconnected!');
});
zConsumer.onEvent("myEvent", (eventData) {
print("myEvent Received");
});
copied to clipboard
ZapSubscriber provides a Stream of ConnectionState enum and a Stream of event received. You can subscribe to specfic event or a group of event or to all event.
final zSubscriber = ZapSubscriber("ws://127.0.0.1:8000/")..connect();
zSubscriber.connectionState.listen((event) {
if (event case ConnectionState.online) {
print("connected!");
}
if (event case ConnectionState.offline) {
print("disconnected!");
}
});
zSubscriber.subscribeToEvent("myEVent").listen((eventData){
print("event received!");
// listen when 'myEvent' is received.
});
zSubscriber.subscribeToEvents(["myEvent", "myOtherEvent"]).listen((event) {
print("a event is received");
// listen when 'myEvent' or 'myOtherEvent' are received.
});
zSubscriber.subscribeToAllEvent().listen((event) {
print("whatever is received!");
// listen all events
});
copied to clipboard
remember cancel stream subscription if you don't need anymore
Sending Event
Both ZapConsumer and ZapSubscriber can send events to the server by the method send
zSubscriber.sendEvent("eventName", "payload");
zConsumer.sendEvent("eventName", "payload");
copied to clipboard
Connect, Disconnect, tryReconnect
connect method is used for both intitial connections and reconnections to the server in case connection lost.
zConsumer.connect();
zSubscriber.connect();
copied to clipboard
disconnect method, close the websocket connection with the server.
zConsumer.disconnect();
zSubscriber.disconnect();
copied to clipboard
clean
When you call disconnect method in a ZapSubscriber you close the webosocket connection but the ZapSubscriber still await events to emits. If for some reason ZapSubscriber is disconnected from a websocket, calling the connect method ZapSubscriber will reconnect with the websocket and can emit the events received from the server, this prevents to create new subscription and prevent event duplicate in case of connection issue.
final zSubscriber = ZapSubscriber("ws://127.0.0.1:8000/")..connect();
zSubscriber.connectionState.listen((event) {
// code here
// still received event after reconnect
});
zSubscriber.subscribeToEvent("myEVent").listen((eventData){
// code here
// still received event after reconnect
});
zSubscriber.disconnect();
zSubscriber.connect();
copied to clipboard
If you want to close completly the ZapSubscriber call method clean, this close and clean the connection with websocket into the ZapSubscriber.
final zSubscriber = ZapSubscriber("ws://127.0.0.1:8000/")..connect();
zSubscriber.connectionState.listen((event) {
// code here
// No received event after clean
});
zSubscriber.subscribeToEvent("myEVent").listen((eventData){
// code here
// No received event after clean
});
zSubscriber.clean(); // all subscriptions "done"
copied to clipboard
Important: cancel all StreamSuscription
Contributions are wellcome! #
What's Next?
✅ event management.
❌ comunication between clients.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.