-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: add remote function query.stream
#14292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Implements `query.batch` to address the n+1 problem
- streams are deduplicated on the client, i.e. same resource+payload == same stream instance - a stream is kept open as long as it's either in a reactive context or someone iterates over it. Streams can close when noone listens anymore and open back up when someone does again - cannot iterate on the server right now, only retrieve the first value via promise - cannot refresh/override (doesn't make sense)
🦋 Changeset detectedLatest commit: cead45e The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
How confident are we about the deduplication part? For cases where you're streaming a live view of some database query, that makes sense, but are there cases more like this? for await (const n of countToTen()) {
console.log(n);
} It would be weird if I did that and it started at 7 because of something happening in an unrelated component. Genuinely not sure what the right way to think about this is. |
yeah I had the same thought already. Hard to tell, both ways make sense - which maybe means we need to give people some way to tell what it is? Update: maybe it should be "each invocation is a separate stream". Then you can still deduplicate yourself if you want to. |
Co-authored-by: Rich Harris <[email protected]>
I see I wonder about the name. I'd expect |
SSEs are just an implementation detail. We could theoretically swap it out for some other mechanism in future (a non-SSE streaming HTTP response, or WebSockets, or IPC, or whatever else depending on platform) and so it wouldn't make sense to bake it into the name of the API. For large files I would expect to just be able to return a So the name doesn't have to be Whatever name we pick would have to make sense for both of these cases: <script>
import { getLikes } from './data.remote';
</script>
<!-- updates in realtime as the server pushes more data -->
<p>likes: {await getLikes()}</p> <script>
import { ask } from './ai.remote';
let { question } = $props();
let answer = $state('');
$effect(async () => {
for await (const chunk of ask(question)) {
answer += chunk;
}
});
</script>
<div>{answer}</div> (very incomplete/buggy example but you get the idea — in one case each chunk is standalone and is thus suitable to be treated like any other query, in the other it's a series of events that need to be handled one at a time) |
Between SSE and web sockets there are differences such as SSE having auto-reconnect. How do you know how to handle a flaky connection without being aware of the underlying implementation? |
The framework takes care of that on your behalf |
I guess retrying with exponential back off would probably work well enough for most websocket applications. If we do want to have websockets as part of |
It would most likely be configured through your adapter |
const stream = oneToTen(); | ||
</script> | ||
|
||
<!-- these are one single ReadableStream request since they share the same stream instance --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ReadableStream
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream - the comment is meant as "these are not two network requests, it's one because they share the same stream instance"
I guess I could've written that instead ... 😅
// TODO how would we subscribe to the stream on the server while deduplicating calls and knowing when to stop? | ||
throw new Error( | ||
'Cannot iterate over a stream on the server. This restriction may be lifted in a future version.' | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we no longer deduplicate, is this still a concern? If you for-awaited an infinite async iterable during render then you'd have the same problem whether it's the client or the server, and the solution is 'don't do that'. So I'm not sure if we need this restriction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true, let me take a look
Even if we consider SSE to be an implementation detail, I still think it could be helpful to mention in the docs or faq so that it's easier to find when people have the question, "how do I do SSE with SvelteKit?" |
It is mentioned, in the inline |
It's probably worth putting " (SSE)" after it as I was searching for the abbreviation and didn't find it |
Co-authored-by: Rich Harris <[email protected]>
WIP, builds on top of the query-batch PR to avoid merge conflicts like crazy since they touch the same files; the batch PR should be merged first
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.