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

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials |  Articles

question

Upvotes
Accepted
1 0 0 0

how to subscribe to more rics on demand on websocket in Python

I’m now trying to subscribe several rics but the it seems once I subscribe to a ric, I cannot subscribe to another:



RECEIVED on session1:

[

{

"ID":2,

"Key":{

"Name":"/0005.HK",

"Service":"ELEKTRON_DD"

},

"State":{

"Code":"AlreadyOpen",

"Data":"Suspect",

"Stream":"Closed",

"Text":"Request Rejected: Cannot request new item on open stream."

},

"Type":"Status"

}

]


I did see in the docs where we can send a list of rics in batch. However, I’d like to append new ric on demand instead of subscribe all in one go.


what i am doing currently (which i believe is incorrect):

session1 = WebSocketTestSession("session1", hostList[0], sts_token, position)
session1.connect()

if session1.logged_in:        
    session1._send_market_price_request('/TRI.N')
    session1._send_market_price_request('/0005.HK')
    session1._send_market_price_request('/0700.HK')
websocketsrrtorefinitiv-realtime-optimised
icon clock
10 |1500

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

Upvotes
Accepted
20.3k 73 10 20

Hi @daniel.yuen

Please work through the tutorials before you start coding your application - starting from the first relevant 'Connect to' tutorial i.e.

Connect to Refinitiv Real-Time - Optimized (cloud connection)

OR

Connect to RTDS (deployed ADS connection)

Working through the tutorials will make things much clearer and hopefully save you time in the long run.

For example, the above tutorials will show you that you should use StreamID:1 for the Login request.

I don't see your code for the Login request, but I suspect you are using 0 or 1 for the Login streamID and then closing the stream when you call _send_close_stream_request with the Login StreamID. When you close the Login Stream - you are logging out and closing the connection.

Also, if you are not interested in streaming data and only want to snapshot the current price for an instrument, you can use the Snapshot feature which will request the instrument and close the stream once you receive the price - saving you the effort of closing the stream.

Snapshot Request

e.,g.

{
  "ID":2,
  "Key":{
    "Name":"VOD.L"
  },
  "Streaming":false
}
icon clock
10 |1500

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

Upvote
20.3k 73 10 20

Hi @daniel.yuen

You need to use a unique Stream ID for each new item you open a stream for. So, you need to keep a StreamID counter or something similar which you can increment etc.

Once you close a stream you can re-use a StreamID if required.

See the Request Data Tutorial for details on StreamID.

icon clock
10 |1500

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

Upvotes
1 0 0 0

You can see that after I subscribed to the first one it worked. then after I subscribed to the 2nd "0700.HK' i received message "

WebSocket Closed for session1" despite with a different ID. I also tried close the first connection first (calling _sending_close_stream_request) but it did not help.


Log messages


RECEIVED on session1:

SENT on session1:

{

"ID":0,

"Key":{

"Name":"/0005.HK",

"Service":"ELEKTRON_DD"

}

}

RECEIVED on session1:

20210511 13:47:44 - /0005.HK: 48.6/48.65, 49.15/49.2

RECEIVED on session1:

20210511 13:47:51 - /0005.HK: 48.6/48.65, 0/0

RECEIVED on session1:

RECEIVED on session1:

SENT on session1:

{

"Type":"Pong"

}

RECEIVED on session1:

RECEIVED on session1:

RECEIVED on session1:

RECEIVED on session1:

SENT on session1:

{

"Type":"Pong"

}

RECEIVED on session1:

20210511 13:48:30 - /0005.HK: 48.6/48.65, 0/0

RECEIVED on session1:

RECEIVED on session1:

RECEIVED on session1:

RECEIVED on session1:

RECEIVED on session1:

SENT on session1:

{

"Type":"Pong"

}

RECEIVED on session1:

SENT on session1:

{

"ID":0,

"Type":"Close"

}

SENT on session1:

{

"ID":1,

"Key":{

"Name":"/0700.HK",

"Service":"ELEKTRON_DD"

}

}

WebSocket Closed for session1

Reconnect to the endpoint for session1 after 3 seconds...

Connecting to WebSocket wss://amer-3-t2.streaming-pricing-api.refinitiv.com:443/WebSocket for session1...

WebSocket successfully connected for session1!


Codes Snippets


session1 = WebSocketTestSession("session1", hostList[0], sts_token, position)
session1.connect()

time.sleep(8)
if session1.logged_in:
    # session1._send_market_price_request('/TRI.N')
    session1._send_market_price_request('/0005.HK')
    #
    time.sleep(60)
    session1._send_close_stream_request(0)

if session1.logged_in:
    session1._send_market_price_request('/0700.HK')


def _send_market_price_request(self, ric_name):
    """ Create and send simple Market Price request """
    mp_req_json = {
        'ID': self.subscription_count,
        'Key': {
            'Name': ric_name,
            'Service': service
        },
    }
    self.subscription_count += 1
    self.web_socket_app.send(json.dumps(mp_req_json))
    print("SENT on " + self.session_name + ":")
    print(json.dumps(mp_req_json, sort_keys=True, indent=2, separators=(',', ':')))

def _send_close_stream_request(self, stream_id):
    """ Create and send simple Market Price request """
    mp_req_json = {
        'ID': stream_id,
        'Type': 'Close',
    }
    self.web_socket_app.send(json.dumps(mp_req_json))
    print("SENT on " + self.session_name + ":")
    print(json.dumps(mp_req_json, sort_keys=True, indent=2, separators=(',', ':')))


icon clock
10 |1500

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

Click below to post an Idea Post Idea