python - Detecting when a client disconnects with Django and Server-Sent Events on Heroku -
heroku's documentation sending streaming response says:
"if you’re sending streaming response, such server-sent events, you’ll need detect when client has hung up, , make sure app server closes connection promptly. "
i've been experimenting on heroku server-sent events in django, using django-sse. uses iterator loops forever, reading messages redis pub/sub channel , sending them client:
def iterator(self): connection = _connect() pubsub = connection.pubsub() pubsub.subscribe(self.get_redis_channel()) message in pubsub.listen(): if message['type'] == 'message': event, data = json.loads(message['data']) self.sse.add_message(event, data) yield
the problem break out of loop if client disconnects, can close connection redis. how can detect when client disconnects?
i had similar problems scala play2 application on heroku wouldn't detect when sse connection being closed client on either close() or refresh or window closing. enabling new heroku labs websockets feature fixed me.
an update:
i asked heroku support , told them experience. reply.
hi lloyd,
this known issue, , correct new websocket endpoints fix problem. don't plan address problem in existing endpoints, rollout websocket endpoints wider audience.
best,
jake
Comments
Post a Comment