Package: zaptools
ZaptoolsA toolkit for Event-Driven websocket managementAlso SupportedLangSideView SourceClient/Serverzaptools_dartGetting StartedZaptools provides tools for building event-driven websocket integration. It includes pre-existing classes to seamless integration with FastApi and Sanic.installationpipinstallzaptools# windowspip3installzaptools# macFastAPIfromfastapiimportFastAPI,WebSocketfromzaptools.toolsimportEventRegister,Contextfromzaptools.connectorsimportFastApiConnectorapp:FastAPI=FastAPI()register:EventRegister=EventRegister()@register.on_event("hello")asyncdefhello_trigger(context:Context):conn=context.connectionawaitconn.send("hello","HELLO FROM SERVER !!!")@app.websocket("/ws")asyncdefwebsocket_endpoint(ws:WebSocket):connector=FastApiConnector(reg,ws)awaitconnector.start()Firstly create aFastAPIandEventRegisterinstance.EventRegisterhas the responsability to create events.fromfastapiimportFastAPI,WebSocketfromzaptools.toolsimportEventRegister,Context,Connectorfromzaptools.adaptersimportFastApiAdapterapp:FastAPI=FastAPI()register:EventRegister=EventRegister()For Creating events use the decorator syntax.
This will creates an event named"hello"and it will callhello_triggerfunction when an event named"hello"is received.@register.on_event("hello")asyncdefhello_trigger(context:Context):conn=context.connectionawaitconn.send("hello","HELLO FROM SERVER !!!")Event it is a class with name("hello") and the callback(hello_trigger)For connectingEventRegisterwith the websocket class provided by FastAPI framework, there is aFastApiConnector, use theplug_and_startstatic method of theFastApiConnector, it will start to receive events.@app.websocket("/")asyncdefwebsocket_endpoint(ws:WebSocket):awaitFastApiConnector.plug_and_start(register,ws)It's the same way for Sanic FrameworkSanicfromsanicimportSanic,Request,Websocketfromzaptools.toolsimportEventRegister,Contextfromzaptools.connectorsimportSanicConnectorapp=Sanic("MyHelloWorldApp")register:EventRegister=EventRegister()@register.on_event("hello")asyncdefhello_trigger(context:Context):conn=context.connectionawaitconn.send("hello","HELLO FROM SERVER !!!")@app.websocket("/")asyncdefwebsocket(request:Request,ws:Websocket):connector=SanicConnector(reg,ws)awaitconnector.start()Context objectEach element is triggered with aContextobject. ThisContextobject contains information about the current event and whichWebSocketConnectionis invoking it.Context.event_name# name of current eventContext.payload# payload the data from the connectionContext.connection# WebSocketConnectionSending EventsIn order to response to the client use theWebSocketConnection.send(event:str, payload:Any), this object is provided by theContext.@register.on_event("hello")asyncdefhello_trigger(context:Context):conn=context.connectionconn.send("hello","HELLO FROM SERVER !!!")# sending "hello" event to client with a payload.WebSocketConnectionWebSocketConnectionprovides a easy interaction with the websocket.WebSocketConnection.id# ID of connectionawaitWebSocketConnection.send(event:str,payload:Any)#Send Event to the clientawaitWebSocketConnection.close()# Close the websocket connectionCoroutines need to be awaited.EventsThe"connected"and"disconnected"events can be used to trigger an action when a connection is started and after it is closed.@register.on_event("connected")asyncdefconnected_trigger(context:Context):print("Connection started")@register.on_event("disconnected")asyncdefdisconnected_trigger(context:Context):print("Connection closed")What's nextsupport for eventsshare message between multiple WebSocketsContributions are wellcome