dan-cooke / remix-sse

Server Sent Events (SSE) for the remix framework
Apache License 2.0
78 stars 3 forks source link

Make `eventKey` default to "message" if not specified #19

Closed dan-cooke closed 8 months ago

dan-cooke commented 8 months ago

@remix-sse/client

Changes the API for useSubscribe to make an eventKey default to 'message'

Using the library myself in a few projects, I have found that I don't usually need to send multiple events from the same emitter, and one eventKey usually suffices. This change will make it more ergonomic for single event emitters, while remaining flexible for multi event emitters

How to migrate

--- const data = useSubscribe('/emitter', 'event-key')
+++ const data = useSubscribe('/emitter', { eventKey: 'event-key'})

Or if you want use the default eventKey: 'message' you can omit it entirely

const data = useSubscribe('/emitter')

@remix-sse/server

Changes the API of the EventStream send function to make the eventKey default to 'message'

Using the library myself in a few projects, I have found that I don't usually need to send multiple events from the same emitter, and one eventKey usually suffices. This change will make it more ergonomic for single event emitters, while remaining flexible for multi event emitters

How to migrate

The first argument of send is now just the data string you wish to send.

export const loader: LoaderFunction = ({ request }) => { return new EventStream(request, (send) => { let gIndex = 0; let g = setInterval(() => { gIndex += 1; --- send('message', JSON.stringify({ hello: 'world', index: gIndex })); +++ send(JSON.stringify({ hello: 'world', index: gIndex })); }, 1000);

return async () => {
  clearInterval(g);
};

}); };

changeset-bot[bot] commented 8 months ago

🦋 Changeset detected

Latest commit: bf6abe9af2a6f8dfe1475926efc01e8cb5939310

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages | Name | Type | | ----------------- | ----- | | @remix-sse/client | Major | | @remix-sse/server | Major |

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