For a deeper look into our Elektron API, look into:

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials |  Articles

question

Upvotes
Accepted
3 3 4 7

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

pythonelektron-sdktreprdp-apiwebsocketsrrto
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hello @mehdi.el.hamzaoui

Thank you for your participation in the forum. Is the reply below satisfactory in resolving your query?

If so please can you click the 'Accept' text next to the appropriate reply. This will guide all community members who have a similar question.

Thanks,

AHS

Upvotes
Accepted
25.3k 87 12 25

Hi @mehdi.el.hamzaoui

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.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

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 Closed

I 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!

Upvotes
17.4k 82 39 63

Hi @mehdi.el.hamzaoui,

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.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thank you, this is answer is very useful. I now understand what I have been doing wrong.

Upvote
16 0 0 1

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,
Brett

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

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"
    }

Never mind yesterday's comment/question. It has been resolved, thank you.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.