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:

NameTypeDescription
clientServerSentEventClientRepresentation of the intercepted EventSource instance.
serverServerSentEventServerActual 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)

  • payload
    • id, 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 the payload argument.

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 source and what you can use it for (listen to messages, close the connection).

Describing REST API

Learn about describing RESTful APIs.