Connection to remote host is lost when using Eikon Messenger Bot API (WebSocket)
Hello,
I need your help. I am using the python demo script for the Eikon Messenger API to listen in a chatroom. Unfortunately, the connection to the remote host is lost after a certain time every time. Is it the WebSocket service or threading in Python or am I getting kicked out by Refinitiv?
Thanks.
Best Answer
-
All,
After investigating this disconnection issue for a while, @Cemil Yigit found that the proxy needs some kind of heartbeat messages or activities every certain minute to keep a WebSocket connection active.
The workaround is sending a ping-pong message with the WebSocket connection as follows:
def on_ping(_, message):
print("Got a ping! A pong reply has already been automatically sent.")
def on_pong(_, message):
print("Got a pong! No need to respond")
web_socket_app = websocket.WebSocketApp(
ws_url,
on_message=on_message,
on_error=on_error,
on_close=on_close,
on_ping=on_ping,
on_pong=on_pong,
subprotocols=['messenger-json'])
wst = threading.Thread(target=web_socket_app.run_forever, kwargs={"sslopt": {"check_hostname": False}, "ping_interval": 60, "ping_timeout": 10, "ping_payload": "2"})The code also needs to handle reconnection logic (as the example application aims for demonstrating the API workflow only, so it is not cover all use cases).
After ping-pong and reconnection logic are applied to the application, the application works fine.2
Answers
-
Hello @Cemil Yigit
Does the WebSocket connection disconnect every 5 minutes? If so, I highly recommend you re-test with the latest version (June 2022 - master branch) of the https://github.com/Refinitiv-API-Samples/Example.MessengerChatBot.Python example.
Please also share the log file too (please remove your credentials before sharing!).
2 -
Yes, it seems, that after 5 Minutes it disconnects. I'll check your link.
0 -
Hello @wasin.w
Thanks for your post. I downloaded the chatbot_demo_ws.py file from git and tried it. Unfortunately, it now lost the connection after 2 minutes. Here ist some output:
Connecting to WebSocket wss://api.collab.refinitiv.com/services/nt/api/messenger/v1/stream ...
2022-09-06 11:10:02: DEBUG:websocket :Connecting proxy...
2022-09-06 11:10:02: INFO:root :Receive: onopen event. WebSocket Connection is established
2022-09-06 11:10:02: INFO:root :Sent: {.......}
2022-09-06 11:12:40: ERROR:root :Error: Connection to remote host was lost.
2022-09-06 11:12:40: ERROR:websocket :error from callback <function on_close at 0x00000273DA7E5DC0>: on_close() takes 1 positional argument but 3 were given
2022-09-06 11:12:40: ERROR:root :Error: on_close() takes 1 positional argument but 3 were given
Refresh Token
2022-09-06 11:14:02: DEBUG:urllib3.connectionpool :Starting new HTTPS connection (1): api.refinitiv.com:443
2022-09-06 11:14:03: DEBUG:urllib3.connectionpool :https://api.refinitiv.com:443 "POST /auth/oauth2/v1/token HTTP/1.1" 200 None
2022-09-06 11:14:03: ERROR:root :send_ws_connect_request exception: Connection is already closed.
2022-09-06 11:14:03: INFO:root :Sent: {.......}
0 -
A log file was not created.
0 -
It's funny, when changing 60 to 180 in the while loop, it seems that is works.
0 -
Hello @Cemil Yigit
Sorry for my mistake, the demo application does not generate a log file. Can you save entire log messages (please removed your credentials) and share a text file in this post?
I am trying to replicate the issue with the chatbot_demo_ws.py example in my environment (Python 3.9, libraries versions are the same as in the ws-requirements.txt file).I did not encounter any disconnection yet (Note: I am connecting with my public internet, not the office environment network).
According to the following logs, are you connecting to the Messenger Bot WebSocket server through any firewall or proxy?
2022-09-06 11:10:02: DEBUG:websocket :Connecting proxy...
2022-09-06 11:10:02: INFO:root :Receive: onopen event. WebSocket Connection is established
2022-09-06 11:10:02: INFO:root :Sent: {.......}
2022-09-06 11:12:40: ERROR:root :Error: Connection to remote host was lost.If so, does your network have any policy that cuts a long connection like WebSocket?
1 -
Hello @wasin.w
Thanks for your support so far.
The hint with the proxy is a good one. I will check it and let you know if it's on my side. In the meantime I adjusted
time.sleep(int(expire_time) - 240)
For the moment, it works fine with 240. If it's not working, I'll let you know and push my code here.
Best, Cemil
2 -
Hello @wasin.w
Unfortunately, the connection to the server was lost after roughly 5 hours. The relevant part of my code looks like this:
if __name__ == "__main__":
# logging settings
log_level = logging.DEBUG
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(name)s - %(message)s:", level=log_level, datefmt="%Y-%m-%d %H:%M:%S")
logging.getLogger("urllib3").setLevel(logging.CRITICAL)
logging.getLogger("websocket").setLevel(logging.CRITICAL)
# rdp authentication token
rdp_token = RDPTokenManagement(bot_username, bot_password, app_key, 30)
access_token, refresh_token, expire_time, logged_in = authen_rdp(rdp_token)
if not access_token:
sys.exit(1)
# join chatrooms
for chatroom in config["chatrooms"].keys():
join_room = join_chatroom(access_token, chatroom)
# connect via a websocket connection
print(f"connecting to {ws_url}")
web_socket_app = websocket.WebSocketApp(ws_url, on_message=on_message, on_error=on_error, on_close=on_close, subprotocols=["messenger-json"])
web_socket_app.on_open = on_open
# infinite event loop
wst = threading.Thread(target=web_socket_app.run_forever, kwargs={"sslopt": {"check_hostname": False}})
wst.start()
try:
while True:
if int(expire_time) > 245:
expire_time = "300"
time.sleep(int(expire_time) - 245)
else:
sys.exit(1)
if not web_socket_app.keep_running:
print(f"{datetime.datetime.now()} new connection established")
send_ws_connect_request(access_token)
print(f"{datetime.datetime.now()} token refreshed")
access_token, refresh_token, expire_time, logged_in = authen_rdp(rdp_token)
if not access_token:
sys.exit(1)
if logged_in:
send_ws_keepalive(access_token)
except KeyboardInterrupt:
web_socket_app.close()So, if you have an idea, please let me know. Thanks.
The part with
if not web_socket_app.keep_running
was added just now.
0
Categories
- All Categories
- 6 AHS
- 37 Alpha
- 161 App Studio
- 4 Block Chain
- 4 Bot Platform
- 16 Connected Risk APIs
- 47 Data Fusion
- 30 Data Model Discovery
- 608 Datastream
- 1.3K DSS
- 577 Eikon COM
- 4.9K Eikon Data APIs
- 7 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- Trading API
- 2.7K Elektron
- 1.3K EMA
- 236 ETA
- 519 WebSocket API
- 33 FX Venues
- 10 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 20 Messenger Bot
- 2 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 59 Open Calais
- 264 Open PermID
- 39 Entity Search
- 2 Org ID
- PAM
- PAM - Logging
- 8.4K Private Comments
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 20 RDMS
- 1.4K Refinitiv Data Platform
- 367 Refinitiv Data Platform Libraries
- 3 Refinitiv Due Diligence
- LSEG Due Diligence Portal API
- 3 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.1K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 10 World-Check Customer Risk Screener
- 990 World-Check One
- 44 World-Check One Zero Footprint
- 45 Side by Side Integration API
- Test Space
- 3 Thomson One Smart
- 1.2K TR Internal
- Global Hackathon 2015
- 2 Specialists Who Code
- 10 TR Knowledge Graph
- 150 Transactions
- 142 REDI API
- 1.7K TREP APIs
- 4 CAT
- 21 DACS Station
- 117 Open DACS
- 1.1K RFA
- 103 UPA
- 172 TREP Infrastructure
- 224 TRKD
- 886 TRTH
- 5 Velocity Analytics
- 5 Wealth Management Web Services
- 59 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛