io.pedestal.websocket

added in 0.7.0

Utilities needed by Pedestal container adapters (such as io.pedestal.jetty) to implement WebSocket support using default Servlet API functionality, as well as utilities for applications that make use Pedestal’s websocket support.

add-endpoint

(add-endpoint container path ws-endpoint-map)

Adds a WebSocket endpoint to a ServerContainer.

The path provides the mapping to the endpoint, and must start with a slash.

The ws-endpoint-map defines callbacks and configuration for the endpoint.

When a connection is started for the endpoint, the :on-open callback is invoked; the return value is saved as the “session object” which is then passed to the remaining callbacks as the first function argument.

:on-open (jakarta.websocket.Session, jakarta.websocket.EndpointConfig)
Invoked when client first opens a connection. The returned value is retained and passed as the first argument of the remaining callbacks.
:on-close (Object, jakarta.websocket.Session, jakarta.websocket.CloseReason)
Invoked when the socket is closed, allowing any resources to be freed.
:on-error (Object, jakarta.websocket.Session, Throwable)
Passed any unexpected exceptions.
:on-text (Object, String)
Passed a text message as a single String.
:on-binary (Object, java.nio.ByteBuffer)
Passed a binary message as a single ByteBuffer.
:configure (jakarta.websocket.server.ServerEndpointConfig.Builder)
Called at initialization time to perform any extra configuration of the endpoint (optional)
:subprotocols (vector of String, optional)
Configures the subprotocols of the endpoint
:idle-timeout-ms (long)
Time in milliseconds before an idle websocket is automatically closed.

All callbacks are optional. The :on-open callback is critical, as it performs all the one-time setup for the WebSocket connection. The on-open-start-ws-connection function is a good starting place.

add-endpoints

(add-endpoints container websockets-map)

Adds all websocket endpoints in the path-map.

on-open-start-ws-connection

(on-open-start-ws-connection opts)

Returns an :on-open callback for add-endpoint, using start-ws-connection to do the actual work.

start-ws-connection

(start-ws-connection ws-session opts)

Starts a simple websocket connection for the given session and config.

Returns a channel used to send messages to the client.

The values written to the channel are either a payload (a String, ByteBuffer, or some object that satisfies the WebSocketSendAsync protocol) or is a tuple of a payload and a response channel.

When the response channel is non-nil, the result of the message send is written to it: Either the keyword :success, or an Exception thrown when attempting to send the message.

Options:

:send-buffer-or-n
Used to create the channel, defaults to 10

WebSocketSendAsync

protocol

members

ws-send-async

(ws-send-async msg remote-endpoint)

Sends msg to remote-endpoint. Returns a promise channel from which the result can be taken.