Support Server-Sent Events (SSE) with adapter-node
See original GitHub issueDescribe the bug
While migrating a sapper app to svelte-kit / adapter-node, I came across a feature that seems to be missing. Namely, the ability to create custom middleware that can handle Server-Sent Events or other long-lived connections that send data to the response stream without closing the connection. Since the handle hook requires returning Response | Promise<Response>
, thus closing the connection, it does not support this. Nor have I found any other way to res.write();
using endpoints.
Information about your SvelteKit Installation:
Diagnostics
System:
OS: Linux 4.4 Ubuntu 20.04.1 LTS (Focal Fossa)
CPU: (8) x64 Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Memory: 7.97 GB / 15.94 GB
Container: Yes
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 14.5.0 - ~/.nvm/versions/node/v14.5.0/bin/node
npm: 6.14.5 - ~/.nvm/versions/node/v14.5.0/bin/npm
npmPackages:
@sveltejs/kit: ^1.0.0-next.68 => 1.0.0-next.68
svelte: ^3.37.0 => 3.37.0
vite: ^2.1.5 => 2.1.5
- Using node-adapter
Severity Adds a bit of friction for migrating from Sapper, reduces feature parity, and will require me to either a) regress to polling the server from the client, or b) write a custom adapter for every project that needs SSEs. (which I’m not sure that this even helps for the development environment anyways)
Additional context Issue #334 seems relevant.
Also originally asked within discord chat but no one seems to know the answer, which is why I am submitting this issue.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:13
- Comments:13 (1 by maintainers)
Top GitHub Comments
@babeard After going through https://vitejs.dev/guide/api-plugin.html#configureserver, I believe we can try the below:
function (req, res, next) => {}
.svelte.config.cjs
’svite.plugins
. (this is forsvelte-kit dev
)adapter-node
that allows polka to use the same middleware. (this is fornode build/index.js
aftersvelte-kit build
with the forkedadapter-node
)I haven’t tested it but I think it should work. There’s a drawback to this approach though, we’d have to match the SSE routes ourselves.
Update
I had tested it, the approach above works though I’m still trying to figure out how we can pass in the middleware to
adapter-node
so that it will configure polka to use them duringsvelte-kit build
step.svelte.config.cjs
./src/middlewares/sse.cjs
as this #3384 changes, are now possible to implement SSE through endpoint?