# Handlers for the stream events def handle_create(data): """The data part of a 'create' event has two lines, ref of the created entity, then its data""" lines = data.split("\n", 2) print("CREATE to: %s\ndata: %s" % (lines[0], lines[1])) def handle_update(data): """The data part of a 'update' event has two lines, ref of the updated entity, then its data""" lines = data.split("\n", 2) print("UPDATE to: %s\ndata: %s" % (lines[0], lines[1])) def handle_delete(data): """The data part of a 'delete' event has one line, ref of the deleted entity""" print("DELETE %s" % (data)) def handle_heartbeat(data): """These messages appear periodically just to keep the stream connection alive.""" print("(beat)") class SseHandlers(object): def __init__(self): # Map event type to handler self.event_handlers = { "create": handle_create, "update": handle_update, "delete": handle_delete, "message": handle_heartbeat } def handleMsg(self, msg): # Get the handler for the event type. Call that handler with the event's data # event_handlers.get(msg.event)(msg.data) self.event_handlers.get(msg.event)(msg.data)