Search code examples
phpgoogle-app-enginewebsocketstreaming

Server Send Events on GAE: ¿is it possible?


I want to enable some sort of realtime mechanism involving PHP (backend) and JS (frontend). And SSE seemed like a nice approach since I don't require bidirectional connections.

The issue I'm facing is that Google App Engine is buffering the PHP response, breaking flush() usage, hence preventing me from building a SSE server.

Does anyone knows how to make GAE NOT to buffer the response? use streaming?sockets? an alternative that is proven to work under GAE? I've tried using channels but that comes with a great challenge since my application has been built entirely bases in PHP and the only thing that I want is to add some realtime notification functionality. And having 2 languages coexist in the same project has it's own complexity too.

Any answer, suggestion, input or feedback will be most appreciated.


Solution

  • EDIT:

    As @Shrike pointed out in a comment the information in this answer is outdated. With new runtimes (python3, Java 11, PHP7) you have a request timeout of 10 minutes with automatic scaling and 24 hours with basic or manual scaling (source: app engine docs).

    Even so it is not possible to use server sent events

    App Engine does not support streaming responses where data is sent in incremental chunks to the client while a request is being processed. All data from your code is collected as described above and sent as a single HTTP response.

    (Source: official google appengine docs)

    legacy answer (for Java 8, PHP 5 und Python 2):

    Technologies like Server Sent Events and Web Sockets will not work on App Engine by design. All front-end requests in App Engine are limited to a runtime of 60 seconds, tasks to ten minutes. So even if you would leave the connection to the client open (like for Server Sent events) it would be closed after 60 seconds. If you could open a web socket connection to a task (which you can't) it would be closed after ten minutes.

    The alternatives are: