From 923f4e209a60364b66a36cd40d9a743ad1802500 Mon Sep 17 00:00:00 2001 From: Andrew Seier Date: Fri, 7 Nov 2014 18:18:33 -0500 Subject: [PATCH] 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. --- plotly/plotly/plotly.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/plotly/plotly/plotly.py b/plotly/plotly/plotly.py index d956f8a4af9..33c58a7a72b 100644 --- a/plotly/plotly/plotly.py +++ b/plotly/plotly/plotly.py @@ -416,6 +416,21 @@ def __init__(self, stream_id): self.stream_id = stream_id self.connected = False + def heartbeat(self, reconnect_on=(200, '', 408)): + """Keep stream alive. Streams will close after ~1 min of inactivity. + + If the interval between stream writes is > 30 seconds, you should + consider adding a heartbeat between your stream.write() calls like so: + >>> stream.heartbeat() + + """ + try: + self._stream.write('\n', reconnect_on=reconnect_on) + except AttributeError: + raise exceptions.PlotlyError("Stream has not been opened yet, " + "cannot write to a closed connection. " + "Call `open()` on the stream to open the stream.") + def open(self): """Open streaming connection to plotly.