Add a Middleware to handle the same logic for polling and websockets