Skip to content

Commit 923f4e2

Browse files
committed
Add a heartbeat method to the py.Stream class (more below)
There are a number of ways to implement this, but this is the most similar to how the rest of the library works. The best approach would be to return messages from the server along with messages generated in the library to give the user as much information as possible.
1 parent 5d86ef4 commit 923f4e2

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

plotly/plotly/plotly.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,21 @@ def __init__(self, stream_id):
416416
self.stream_id = stream_id
417417
self.connected = False
418418

419+
def heartbeat(self, reconnect_on=(200, '', 408)):
420+
"""Keep stream alive. Streams will close after ~1 min of inactivity.
421+
422+
If the interval between stream writes is > 30 seconds, you should
423+
consider adding a heartbeat between your stream.write() calls like so:
424+
>>> stream.heartbeat()
425+
426+
"""
427+
try:
428+
self._stream.write('\n', reconnect_on=reconnect_on)
429+
except AttributeError:
430+
raise exceptions.PlotlyError("Stream has not been opened yet, "
431+
"cannot write to a closed connection. "
432+
"Call `open()` on the stream to open the stream.")
433+
419434
def open(self):
420435
"""Open streaming connection to plotly.
421436

0 commit comments

Comments
 (0)