S
SolidJS7d ago
SinV

Detect when client closes a connection that is being streamed from a server function

I have a server function that, when called from the client, yields data back to the client from a generator. This data stream is supposed to be continuous over time, with the restriction that the user can make a request that stops the stream (via a server function and in-memory cache). The problem I currently am facing is that if the user disconnects (leaves the page, drops connection/internet somehow), the stream remains open (server logs verify it still attempts to send data back). Is there a way in Solid/Start to detect that the HTTP stream from the client has been closed?
3 Replies
SinV
SinVOP7d ago
(for reference, I recognize that this specific case is most likely more privy to WebSockets to easily detect when the client connection is closed, but I'm interested in learning more about the HTTP Streaming abilities within SolidStart)
peerreynders
peerreynders7d ago
On Node event.nativeEvent.node.req returned from getRequestEvent() gives you access to the underlying IncomingMessage where you can register a handler to be notified of the client closing. That said I have to wonder whether using server functions in this manner is outside of their intended use case; a simple RPC abstraction (i.e. even streamed results should be short term/finite so that a client closing doesn't really matter). So while seroval in principle seems to support this being a minority use case perhaps plan B should be to use an API Route and take control over the response on a lower level for a streaming response or perhaps even SSE for a quasi-infinite stream.
web.dev
Streams—The definitive guide  |  Articles  |  web.dev
The Streams API allows JavaScript to programmatically access streams of data received over the network and process them as desired.
GitHub
solid-start-sse-chat/src/server/event-stream.ts at d2b9070f956947c9...
Basic Chat demonstration with server-sent events (SSE) - peerreynders/solid-start-sse-chat
GitHub
[Bug?]: Streaming/ async generator doesn't work · Issue #1477 · so...
Duplicates I have searched the existing issues Latest version I have tested the latest version Current behavior 😯 It shows a error when I consume data from a server function Expected behavior 🤔 Sho...
SinV
SinVOP7d ago
Thanks for your input! I agree, my usecase does sort of exceed the intended use of server functions. I'll have a look at that Node API, but I probably will move over to something like SSE as it much better fits the usecase. Thanks for the info, and I appreciate your response!
Want results from more Discord servers?
Add your server