Package: lightstreamer-client-lib
Lightstreamer Python Client SDKLightstreamer Python Client SDK enables any Python application to communicate bidirectionally with aLightstreamer Server. The API allows to subscribe to real-time data pushed by the server and to send any message to the server.The library offers automatic recovery from connection failures, automatic selection of the best available transport, and full decoupling of subscription and connection operations. It is responsible of forwarding the subscriptions to the Server and re-forwarding all the subscriptions whenever the connection is broken and then reopened.InstallationYou can install the Lightstreamer Client SDK fromPyPI:python -m pip install lightstreamer-client-libThe sdk is supported on Python 3.7 and above.QuickstartTo connect to a Lightstreamer Server, aLightstreamerClientobject has to be created, configured, and instructed to connect to the Lightstreamer Server.
A minimal version of the code that creates a LightstreamerClient and connects to the Lightstreamer Server onhttps://push.lightstreamer.comwill look like this:fromlightstreamer.clientimport*client=LightstreamerClient("http://push.lightstreamer.com/","DEMO")client.connect()For each subscription to be subscribed to a Lightstreamer Server aSubscriptioninstance is needed.
A simple Subscription containing three items and two fields to be subscribed inMERGEmode is easily created (seeLightstreamer General Concepts):sub=Subscription("MERGE",["item1","item2","item3"],["stock_name","last_price"])sub.setDataAdapter("QUOTE_ADAPTER")sub.setRequestedSnapshot("yes")client.subscribe(sub)Before sending the subscription to the server, usually at least oneSubscriptionListeneris attached to the Subscription instance in order to consume the real-time updates. The following code shows the values of the fieldsstock_nameandlast_priceeach time a new update is received for the subscription:classSubListener(SubscriptionListener):defonItemUpdate(self,update):print("UPDATE "+update.getValue("stock_name")+" "+update.getValue("last_price"))# other methods...sub.addListener(SubListener())Below is the complete Python code:fromlightstreamer.clientimport*sub=Subscription("MERGE",["item1","item2","item3"],["stock_name","last_price"])sub.setDataAdapter("QUOTE_ADAPTER")sub.setRequestedSnapshot("yes")classSubListener(SubscriptionListener):defonItemUpdate(self,update):print("UPDATE "+update.getValue("stock_name")+" "+update.getValue("last_price"))sub.addListener(SubListener())client=LightstreamerClient("http://push.lightstreamer.com","DEMO")client.subscribe(sub)client.connect()LoggingTo enable the internal client logger, create aLoggerProviderand set it as the default provider ofLightstreamerClient.importsys,logginglogging.basicConfig(level=logging.DEBUG,format="%(message)s",stream=sys.stdout)loggerProvider=ConsoleLoggerProvider(ConsoleLogLevel.DEBUG)LightstreamerClient.setLoggerProvider(loggerProvider)CompatibilityCompatible with Lightstreamer Server since version 7.4.0.DocumentationLive demosAPI ReferenceSupportFor questions and support please use theOfficial Forum. The issue list of this page isexclusivelyfor bug reports and feature requests.LicenseApache 2.0