Skip to content

Latest commit

 

History

History
111 lines (80 loc) · 2.99 KB

File metadata and controls

111 lines (80 loc) · 2.99 KB
order 5
title sse
description Intercept Server-Sent Events.
keywords
sse
event
server
handler
api

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>)

import { PageCard } from '../../../components/react/pageCard' import { CodeBracketSquareIcon } from '@heroicons/react/24/outline'

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)

  • 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).

Related materials

import { NodejsIcon } from '../../../components/react/icons/nodejs'