- Introduction
- Quick start
- Philosophy
- Comparison
- Default behaviors
- Limitations
- Debugging runbook
- FAQ
- Mocking HTTP
- Mocking SSE
- Mocking GraphQL
- Mocking WebSocket
- Integrations
- API
- CLI
- Best practices
- Recipes
sse
Intercept Server-Sent Events.
The sse function is a subset of the http namespace that helps you create request handlers to intercept Server-Sent Events.
Call signature
sse<EventMap, Params>(predicate: Path, resolver: ServerSentEventResolver<EventMap, Params>)sse.ts
Source code for the `sse` function.
Resolver argument
In addition to all the arguments exposed by the http namespace, the sse handler has the following response resolver properties:
| Name | Type | Description |
|---|---|---|
client | ServerSentEventClient | Representation of the intercepted EventSource instance. |
server | ServerSentEventServer | Actual server connection object. |
ServerSentEventClient
The ServerSentEventClient object represents the intercepted EventSource instance. You use this object to send mock messages to the client, close or error the underlying connection.
.send(payload)
payloadid,string(Optional), a custom event ID.data,string, the sent data of this event.event,string(Optional), a custom event type.retry,number(Optional), a custom reconnection time. If this option is provided, no other properties must be set on thepayloadargument.
Sends a mocked event to the client.
// Send a default "message" event.
client.send({ data: 'hello world' })
// Send a custom "greeting" event.
client.send({
event: 'greeting'
data: 'Hello, John!',
})
// Send an event with a custom ID.
client.send({
id: 'abc-123',
event: 'greeting'
data: 'Hello, John!',
}).error()
Errors the underlying HTTP connection.
client.error().close()
Gracefully closes the underlying HTTP connection.
client.close()ServerSentEventServer
The ServerSentEventClient object represents a connection to the actual server. Use it to establish that connection and listen to the received events from the server.
.connect()
…TODO
const source = server.connect()…TODO What is returned as
sourceand what you can use it for (listen to messages, close the connection).
Related materials
Describing REST API
Learn about describing RESTful APIs.