Skip to content
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

Refactor SSE callback to handle async operations #6

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/server/sse-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type SSECallback = (
* @example
* { lastEventId: '12345' }
*/
context: {lastEventId: string | null}
) => void | Promise<void> | (() => void);
context: { lastEventId: string | null },
) => void | Promise<void> | (() => void) | Promise<() => void> | (() => Promise<void>);

/**
* Creates a Server-Sent Events (SSE) handler for Next.js.
Expand All @@ -44,6 +44,20 @@ type SSECallback = (
* - `close`: A function to close the SSE connection.
* - `context`: An object containing the last event ID received by the client. Not null if the client has been reconnected.
* The callback can return a cleanup function that will be called when the connection is closed.
*
* **HINT:**
* Be sure to **NOT** await long running operations in the callback, as this will block the response from being sent initially to the client. Instead wrap your long running operations in a async function to allow the response to be sent to the client first.
*
* @example
```
export const GET = createSSEHandler((send, close) => {
const asyncStuff = async () => {
// async
};

asyncStuff();
});
```
*
* @returns A function that handles the SSE request. This function takes a `NextRequest` object as an argument.
*
Expand All @@ -63,7 +77,7 @@ export function createSSEHandler(callback: SSECallback) {
let messageId = 0;

const stream = new ReadableStream({
start(controller) {
async start(controller) {
const send: SendFunction = (data: any, eventName?: string) => {
if (!isClosed) {
let message = `id: ${messageId}\n`;
Expand All @@ -86,7 +100,7 @@ export function createSSEHandler(callback: SSECallback) {
}
}

const result = callback(send, close, {lastEventId: request.headers.get('Last-Event-ID')});
const result = await callback(send, close, { lastEventId: request.headers.get('Last-Event-ID') });
if (typeof result === 'function') {
cleanup = result;
}
Expand Down
Loading