This plugin adds fx.sse(...) to create an SSE (Server-Sent Events) connection using EventSource.
import { Fexios } from 'fexios'
import { pluginSSE } from 'fexios/plugins'const fx = new Fexios({ baseURL: 'https://example.com' }).plugin(pluginSSE)
const es = await fx.sse('/events', {
query: { room: 'lobby' },
timeout: 30_000,
})
es.addEventListener('message', (event) => {
console.log('message:', event.data)
})- Returns:
Promise<EventSource>(resolves after the connection is opened) - options.query: query params to merge into the URL
- options.timeout: connect timeout in ms (default:
fx.baseConfigs.timeout ?? 60000)
The plugin emits the following Fexios lifecycle events:
sse:beforeConnect— allow you to modify{ url, timeout }before connectingsse:opensse:messagesse:error
In Node.js, EventSource is not always available globally. You can provide a polyfill:
import EventSource from 'eventsource'
;(globalThis as any).EventSource = EventSource