Back

SSE responses are not supported in the function

  • 0
  • Functions
  • Realtime
Nour
22 Nov, 2023, 15:32

I noticed that the Appwrite Python function uses Flask for its construction, so I attempted to send chunks with Flask’s SSE response. However, the Appwrite function responses do not support the use of return Response(event_stream(), mimetype="text/event-stream")

TL;DR
The issue is that the Appwrite Functions currently do not support server-sent events (SSE) and do not allow for chunked responses. Setting the MIME type to `text/event-stream` is not possible in the Appwrite server. The suggested solutions are to use the existing inbuilt functions provided by Appwrite or to try Flask's SSE response, but neither of these options work. Unfortunately, there is no known solution or timeline for when SSE support will be available in Appwrite Functions.
Ketan
22 Nov, 2023, 17:58

There are inbuilt function to return value. You can check the example from the official docs:

https://appwrite.io/docs/products/functions/development#response

Ketan
22 Nov, 2023, 17:58
TypeScript
def main(context):
    type = context.req.query['type']

    if type == 'empty':
        return context.res.empty()
    elif type =='json':
        return context.res.json({"type": "This is a JSON response"})
    elif type == 'redirect':
        return context.res.redirect("https://appwrite.io", 301)
    elif type == 'html':
        return context.res.send("<h1>This is an HTML response</h1>", 200, {
            "content-type": "text/html"
        })
    else:
        return context.res.send("This is a text response")
Nour
23 Nov, 2023, 06:59

I was aware of that, but none of them support the SSE Response, which I require.

Nour
23 Nov, 2023, 07:33

I tried the next function:```python import time

def main(context): context.log("Starting")

TypeScript
def event_stream():
    context.log("I am inside the Function")
    for i in range(0, 20):
        yield 'data: {"message": "Hello ' + "{}".format(i) + '"}\n\n'
        time.sleep(0.3)
    yield 'data: {"message": "World"}\n\n'
    yield 'data: {"message": "stop"}\n\n'
    yield 'data: {"message": "World"}\n\n'
    yield 'data: {"message": "World"}\n\n'
    yield 'data: {"message": "World"}\n\n'
# Response(event_stream(), mimetype="text/event-stream")
return context.res.send(event_stream(), 200, {"content-type": "text/event-stream"})
TypeScript
Nour
23 Nov, 2023, 07:35

Its works but It send all yield message as one response only.

Nour
23 Nov, 2023, 07:50

I believe this is happening because the Appwrite server doesn't support setting the MIME type to text/event-stream, which is necessary to enable server-sent events on both the client and server side.

Nour
26 Nov, 2023, 05:57

???

ideclon
26 Nov, 2023, 06:41

Yes, this is correct. Appwrite Functions do not currently support chunked responses. My understanding is that this is on the wishlist, but I'm not aware of any open Issue for it, or of any specific milestone (unless this is part of the SSR milestone for 1.5? - would need someone from Core to clarify that).

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more