Package: dataRT
dataRT Python LibraryAboutWhat does dataRT do?dataRT is a drop-in replacement for the InfluxDBClient which writes data to InfluxDB while simultaneously echoing the points to a WebSocket. This allows you to view the data in real-time in a web browser or other client.What scenario would I need this for?dataRT is designed to be used where the InfluxDBClient is already being used but constantly querying InfluxDB does not provide adequate real-time access to your data. By creating a client webpage you can display the datain real timewhile simultaneously recording the points in InfluxDB for examination at a later date.What if I don't want to use InfluxDB?No problem!
InfluxDB isnota requirement to use dataRT. If theinflux_urlargument is set toNone, dataRT will echo the data to a WebSocket, but not attempt to send data to an InfluxDB instance.InstallationPyPi>pipinstalldataRTSetuptools>pythonsetup.pyinstallUsageSingle-Thread ApplicationIf you are constrained for resources, you can use theCoreClientobject.It is highly recommended that youmonkey-patchusing gevent when using this technique.To start, import theCoreClient:fromdataRTimportCoreClientThen create a client:client=CoreClient(influx_url=None,debug=True)TheCoreClienttakes these arguments, all are optional:KeywordDefaultDescriptionhost'0.0.0.0'Bind host for the WebSocketport8080Bind port for the WebSocketinflux_url'localhost'URL for InfluxDB. Note: if this is set toNoneno attempt to connect to an InfluxDB instance will be madeinflux_port8086Port for InfluxDBinflux_userNoneUsername for InfluxDBinflux_passNonePassword for InfluxDBinflux_db'example'Database for InfluxDBwebsocket_pathr'/ws'Base path for accessing WebSocket resourcesdebugFalseShow debug messagesDefine a "action" function then start the dataRT loop. This function will be constantly called as the dataRT runs through its maintenance loop:defaction(possible_arg,possible_kwarg=100):print(possible_arg)print(possible_kwarg)client.run_in_loop(action,'possible_arg',possible_kwarg=1000)Oncerun_in_loopis called, the client blocks untilclient.stop()is called or the program exits.Available client functions:MethodArgumentsDescriptionwrite_pointspoints,*args,**kwargsWrites points to the WebSocket and InfluxDB instance.pointsare used for the WebSocket, all other args are passed to the InfluxDBClientquery*args,**kwargsPasses a query to the InfluxDBClientcreate_database*args,**kwargsPasses acreate_databaseto the InfluxDBClientserve_foreverNoneBlocks and serves the WebSocket foreverstartNoneStarts the WebSocket serverstopNoneStops the WebSocket serverrun_in_loopactions,*args,**kwargsBlocks and serves the WebSocket forever, but calls theactionsfunction every loop with the provided*argsand**kwargsAvailable client properties:PropertyCan be set?TypeDescriptionis_servingNoboolReturnsTrueif the WebSocket is currently being servedinflux_clientNoInfluxDBClientReturns the InfluxDBClient object to be used with more complex InfluxDB operationsws_on_messageYesfunctionFunction to call when the WebSocket receives a new message. Should accept 2 args for the WebSocket client and the message (string)ws_on_connectYesfunctionFunction to call when the WebSocket connects. Should accept 1 arg for the WebSocket clientws_on_disconnectYesfunctionFunction to call when the WebSocket disconnects. Should accept 1 arg for the WebSocket clientMulti-Process ApplicationIf you need more reliability or want to avoid your code possibly blocking dataRT background tasks, use theClientobject.To start, import theClient:fromdataRTimportClientThen create a client:client=Client(influx_url=None,debug=True)TheClienttakes these arguments, all are optional:KeywordDefaultDescriptionhost'0.0.0.0'Bind host for the WebSocketport8080Bind port for the WebSocketinflux_url'localhost'URL for InfluxDB. Note: if this is set toNoneno attempt to connect to an InfluxDB instance will be madeinflux_port8086Port for InfluxDBinflux_userNoneUsername for InfluxDBinflux_passNonePassword for InfluxDBinflux_db'example'Database for InfluxDBwebsocket_pathr'/ws'Base path for accessing WebSocket resourcesdebugFalseShow debug messagesOnce the client is defined, you can start it:client.start()Oncestartis called, the client will start in another process and continue serving untilclient.stop()is called or the program exits.Available client functions:MethodArgumentsDescriptionwrite_pointspointsWrites points to the WebSocket and InfluxDB instancestartNoneStarts the WebSocket serverstopNoneStops the WebSocket serverWhere is my WebSocket?The WebSocket associates itself with theclientroute on the WebSocket path:ws:\\<host>:<port>\<ws route>\client.For the default settings, this would bews:\\0.0.0.0:8080\ws\clientExamplesExamples can be found in theexamplesdirectory.Library Usagegevent_example.pyshows how to integrate the dataRT client in a single-threaded application using gevent. (Note: using this method you must be careful not to block the process from running maintenance for the WebSocket connection otherwise you may encounter issues)multiprocess_example.pyshows how to use the dataRT client as a drop-in-place replacement for the InfluxDBClient by leveraging a separate process (Note: This does consume more system resources, use the gevent method if you are working in a resource-constrained environment)HTML/JSview_data.htmluses basic jQuery to write each piece of data that is received onto the page in a table.plot_data.htmluses theChart.jslibrary to plot each point onto a line graph in real time.LicensingThis project is licensed under the MIT license.
No warranty is provided, use at your own risk.