Keeping connection alive with heartbeat in Python

Created at 15 May 2021, 11:08
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
VI

vihar

Joined 17.01.2021

Keeping connection alive with heartbeat in Python
15 May 2021, 11:08


Hi,

I am using spotware_connect library in Python to download historical ticks through OpenAPI.

To prevent disconnecting when I am not sending requests for a longer period, I am sending 
heartbeat messages every 10 seconds with emit("HeartbeatEvent", payloadType=51)

However I still get disconnected when I try to request data after a longer period of 
inactivity (1-2 minute). The server sends a heartbeat too, but then I am disconnected, 
and get a message with errorCode: "INVALID_REQUEST", description: 'Trading account is not authorized'.

Why are the HeartbeatEvent I am sending do not work?

Thanks,
Gabor

@vihar
Replies

amusleh
17 May 2021, 09:56

Hi,

If you are getting Trading Account is not authorized error from server it means you are connected, so there is no issue with heartbeat or your connection, otherwise you will not be able to receive that error.

Your issue is with trading account authorization not connection, you have to send an account authorization request for each trading account you use on a connection, otherwise if you try to send other messages related to a trading account that is not authorized for that connection you will receive that error.

There is also token expiration or refresh that can de-authorize your authorized account, while your account is authorized for a connection and your access token expire or you refresh the access token then you have to re-authorize that account, to guard against this you can use invalid token message, and whenever it happened you can re-authorize the account. 


@amusleh

opusensemble
01 Aug 2021, 20:00

RE:

Hi amusleh, 

Where can the heartbeat documentation or a usage example for Open API be found?

Looking forward to hearing back from you, 
Best Regards, 


@opusensemble

amusleh
02 Aug 2021, 07:53

RE: RE:

opusensemble said:

Hi amusleh, 

Where can the heartbeat documentation or a usage example for Open API be found?

Looking forward to hearing back from you, 
Best Regards, 

Hi,

Please read the documentation connection tutorial.


@amusleh

andychen
13 Sep 2023, 11:18 ( Updated at: 13 Sep 2023, 13:13 )

RE: RE: RE:

amusleh said: 

opusensemble said:

Hi amusleh, 

Where can the heartbeat documentation or a usage example for Open API be found?

Looking forward to hearing back from you, 
Best Regards, 

Hi,

Please read the documentation connection tutorial.

Hi amusleh,

The connection tutorial link https://spotware.github.io/open-api-docs/connection/ is broken. Could you please share again.
I'm having issue sending the heartbeat request using the python sdk.

Thanks in advance.

Regards

Andy

 


@andychen

amusleh
14 Sep 2023, 05:58 ( Updated at: 14 Sep 2023, 06:34 )

RE: RE: RE: RE:

andychen said: 

amusleh said: 

opusensemble said:

Hi amusleh, 

Where can the heartbeat documentation or a usage example for Open API be found?

Looking forward to hearing back from you, 
Best Regards, 

Hi,

Please read the documentation connection tutorial.

Hi amusleh,

The connection tutorial link https://spotware.github.io/open-api-docs/connection/ is broken. Could you please share again.
I'm having issue sending the heartbeat request using the python sdk.

Thanks in advance.

Regards

Andy

 

Hi,

Open API documentation is moved to cTrader help portal: Establishing a Connection - Open API (ctrader.com)


@amusleh

PanagiotisChar
14 Sep 2023, 06:29 ( Updated at: 14 Sep 2023, 06:34 )

Hi Andy, 

The documentation has been moved here

https://help.ctrader.com/open-api/connection/


@PanagiotisChar