Python example WebSocket API

Hi,
I would like to run the Python example for market price from the streamingtools folder.
Let's take this function:
def process_message(ws, message_json):
""" Parse at high level and output JSON of message """
message_type = message_json['Type']
........
What do the arguments "ws" and message_json" entail here? What should I input there?
Thanks in advance
Best Answer
-
If you have not already done so, I recommend you work through the Quick Start guide for Websocket API.
There are two versions of the Quick Start - one for connecting to your TREP / ADS and for a Elektron Cloud connection - please refer to the appropriate one.
Before referring to the streamingtools examples, it may better to have a play around with the WebSocket API Try it Now! Documentation - as this will allow to easily understand the different JSON request messages you can use to login, request data - as well as the JSON responses you can receive from the server.
It may also be worth referring to the documentation for the python websocket client documentation - if you are not familiar with how the python Websocket library works. The process_message method you refer to is actually called from the on_message method which is a callback handler specified when creating the WebSocketApp, to handle any incoming messages from the Websocket connection.
If you follow the streamingtools example code flow, you will see that it creates a websocket connection and specifies various callback handlers e.g. on_message, on_open etc.
Once the connection is open, the on_open method calls the send_login_request method to send a login request to the server.
When a message is received back from the server, the on_message method dumps the JSON response and calls the process_message method, which checks for a valid login response and if so, it then calls the process_login_response method which simply sends a data request to the server - requesting 'TRI.N' MarketPrice data.
Assuming the server responds (asynchronously) with data, the on_message method is called again which dumps the data (JSON payload) to the console.
As the data request is by default a streaming request, the on_message method will be called asynchronously as and when their is market activity and the server sends updates over the websocket connection.
You will note that the process_message method also checks each message received for a Ping and if so, responds with a Pong. This ensures the heartbeat is maintained with the server and the connection is kept alive.
0
Answers
-
Looking at the market_price.py example within the streamingtools folder, the 'ws' parameter is the actual WebSocket object used to communicate with the server. The 'message_json' parameter is the data that has arrived from that server.
The process_message(ws, message_json) function is called by another function that captures events received from the server. When you try the example, you simply need to specify the address of the server and possibly user. This is set at the top of the market_price.py example. You don't input the 'ws' nor 'message_json' arguments directly.
Note: To use this example, you must have a market data server setup (called ADS) within your environment that supports the WebSocket service, i.e. ADS 3.2 or greater.
0 -
Hello @mehdi.el.hamzaoui,
The Python examples for the WebsocketAPI utilize the websocket-client library.For the process_message function, the ws parameter represents a WebSocketApp object from the websocket-client library (more details on GitHub here), which acts as a means of sending JSON response after processing.
The message_json parameter represents JSON data that the python example has received as a response from an ADS. Below is an example of what this may look like for a Login Refresh message. More examples of response data can be found in the WebsocketAPI documentation:[
{
"ID": 1,
"Type": "Refresh",
"Domain": "Login",
"Key": {
"Name": "user",
"Elements": {
"AllowSuspectData": 1,
"ApplicationId": "256",
"ApplicationName": "ADS",
"Position": "127.0.0.1",
"ProvidePermissionExpressions": 1,
"ProvidePermissionProfile": 0,
"SingleOpen": 1,
"SupportEnhancedSymbolList": 1,
"SupportOMMPost": 1,
"SupportPauseResume": 1,
"SupportStandby": 0,
"SupportBatchRequests": 7,
"SupportViewRequests": 1,
"SupportOptimizedPauseResume": 1
}
},
"State": {
"Stream": "Open",
"Data": "Ok",
"Text": "Login accepted by host localhost."
},
"Elements": {
"PingTimeout": 30,
"MaxMsgSize": 61430
}
}
]Regards,
Brett0 -
Thank you, this is answer is very useful. I now understand what I have been doing wrong.
0 -
Hi, thank you for your reponse.
I now do get this JSON response.
However when I want to retrieve internal data from our TREP via Market Price "Single" input, which is:
{
"ID": 2,
"Key": {
"Service": "ATS",
"Name": "AT0000383864=MKV",
}
}I get the following response:
},
"State": {
"Stream": "Closed",
"Data": "Suspect",
"Code": "NotEntitled",
"Text": "A21: An unknown User Handle has been supplied\n"
}0 -
Hi @umer.nalla,
Thank you for your elaborate and helpful response. Meanwhile I have been able to get a login request response as well as market price request response via the interactive user guide. However, when running the Python code after filling in the global default variables, I get the following response:
Connecting to WebSocket ws://lrb14071.europe.intranet:15000/WebSocket ...
Handshake status 500 handshakefailed
WebSocket ClosedI fail to see what I am doing wrong here. The interactive guide clearly shows that there is a connection and that the WebSocket API works. Suggestions?
Thanks!
0 -
Never mind yesterday's comment/question. It has been resolved, thank you.
0
Categories
- All Categories
- 6 AHS
- 36 Alpha
- 166 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 682 Datastream
- 1.4K DSS
- 613 Eikon COM
- 5.2K Eikon Data APIs
- 10 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- 3 Trading API
- 2.9K Elektron
- 1.4K EMA
- 248 ETA
- 552 WebSocket API
- 37 FX Venues
- 14 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 23 Messenger Bot
- 3 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 275 Open PermID
- 44 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 22 RDMS
- 1.9K Refinitiv Data Platform
- 626 Refinitiv Data Platform Libraries
- 5 LSEG Due Diligence
- 1 LSEG Due Diligence Portal API
- 4 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.2K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 12 World-Check Customer Risk Screener
- 1K World-Check One
- 46 World-Check One Zero Footprint
- 45 Side by Side Integration API
- 2 Test Space
- 3 Thomson One Smart
- 10 TR Knowledge Graph
- 151 Transactions
- 143 REDI API
- 1.8K TREP APIs
- 4 CAT
- 26 DACS Station
- 121 Open DACS
- 1.1K RFA
- 104 UPA
- 191 TREP Infrastructure
- 228 TRKD
- 915 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 83 Workspace SDK
- 11 Element Framework
- 5 Grid
- 18 World-Check Data File
- 1 Yield Book Analytics
- 46 中文论坛