Package: rockets
Rockets Python ClientA small client forRocketsusingJSON-RPCas
communication contract over aWebSocket.Table of ContentsInstallationUsageConnectionNotificationsRequestsBatchingInstallationYou can install this package fromPyPI:pipinstallrocketsUsageClientvs.AsyncClientRockets provides two types of clients to support asychronous and synchronous usage.TheAsyncClientexposes all of its functionality asasyncfunctions, hence anasyncioevent loopis needed to complete pending
execution viaawaitorrun_until_complete().For simplicity, a synchronousClientis provided which automagically executes in a synchronous,
blocking fashion.ConnectionCreate a client and connect:fromrocketsimportClient# client does not connect during __init__;# either explicit or automatically on any notify/request/sendclient=Client('myhost:8080')client.connect()print(client.connected())Close the connection with the socket cleanly:fromrocketsimportClientclient=Client('myhost:8080')client.connect()client.disconnect()print(client.connected())Server messagesListen to server notifications:fromrocketsimportClientclient=Client('myhost:8080')client.notifications.subscribe(lambdamsg:print("Got message:",msg.data))NOTE: The notification object is of typeNotification.Listen to any server message:fromrocketsimportClientclient=Client('myhost:8080')client.ws_observable.subscribe(lambdamsg:print("Got message:",msg))NotificationsSend notifications to the server:fromrocketsimportClientclient=Client('myhost:8080')client.notify('mymethod',{'ping':True})RequestsMake a synchronous, blocking request:fromrocketsimportClientclient=Client('myhost:8080')response=client.request('mymethod',{'ping':True})print(response)Handle a request error:fromrocketsimportClient,RequestErrorclient=Client('myhost:8080')try:client.request('mymethod')exceptRequestErroraserr:print(err.code,err.message)NOTE: Any error that may occur will be aRequestError.Asynchronous requestsMake an asynchronous request, using theAsyncClientandasyncio:importasynciofromrocketsimportAsyncClientclient=AsyncClient('myhost:8080')request_task=client.async_request('mymethod',{'ping':True})asyncio.get_event_loop().run_until_complete(request_task)print(request_task.result())Alternatively, you can useadd_done_callback()from the returnedRequestTaskwhich is called
once the request has finished:importasynciofromrocketsimportAsyncClientclient=AsyncClient('myhost:8080')request_task=client.async_request('mymethod',{'ping':True})request_task.add_done_callback(lambdatask:print(task.result()))asyncio.get_event_loop().run_until_complete(request_task)If theRequestTaskis not needed, i.e. nocancel()oradd_progress_callback()is desired, use
therequest()coroutine:importasynciofromrocketsimportAsyncClientclient=AsyncClient('myhost:8080')coro=client.request('mymethod',{'ping':True})result=asyncio.get_event_loop().run_until_complete(coro)print(result)If you are already in anasyncfunction or in a Jupyter notebook cell, you may useawaitto
execute an asynchronous request:# Inside a notebook cell hereimportasynciofromrocketsimportAsyncClientclient=AsyncClient('myhost:8080')result=awaitclient.request('mymethod',{'ping':True})print(result)Cancel a request:fromrocketsimportAsyncClientclient=AsyncClient('myhost:8080')request_task=client.async_request('mymethod')request_task.cancel()Get progress updates for a request:fromrocketsimportAsyncClientclient=AsyncClient('myhost:8080')request_task=client.async_request('mymethod')request_task.add_progress_callback(lambdaprogress:print(progress))NOTE: The progress object is of typeRequestProgress.BatchingMake a batch request:fromrocketsimportClient,Request,Notificationclient=Client('myhost:8080')request=Request('myrequest')notification=Notification('mynotify')responses=client.batch([request,notification])forresponseinresponses:print(response)Cancel a batch request:fromrocketsimportAsyncClientclient=AsyncClient('myhost:8080')request=Request('myrequest')notification=Notification('mynotify')request_task=client.async_batch([request,notification])request_task.cancel()