Skip to content
Last updated

Let readers try WebSocket channels

Plans:ProScaleEnterprise

The Try it button on a WebSocket channel opens Replay. In Replay, readers connect from their browser straight to your server, send the channel's messages, and read the replies. Because the browser connects directly, the connection works only when your description, your server, and your project allow it.

Early access

This is a feature in early access. It may not be available for your organization yet, and its behavior may change before general availability.

To request access to this feature, contact our Support team.

Display the Try it button on WebSocket channels

A channel displays the Try it button when your description says that the channel uses WebSocket:

  • One of the channel's servers uses the ws or wss protocol. A channel that lists no servers uses every server of the description.
  • The description declares no servers, and the channel has a ws binding. Readers then enter the server address in Replay.

The channel also needs at least one message with a payload or an example. Other channels, such as Kafka or AMQP channels, have no Try it button.

The following example declares a WebSocket channel in a description without servers:

channels:
  liveOrders:
    address: /orders/live
    bindings:
      ws: {}

Let the browser connect

  • Declare the server with protocol: wss. Browsers block ws:// connections from docs pages served over HTTPS.
  • If your server checks the Origin header, allow your project's domain.
  • If your project sends a Content-Security-Policy header, add your server to its connect-src directive.

Declare credentials a browser can send

In a browser, Replay can set only the address and the subprotocols of a WebSocket connection. It cannot add other headers, cookies, or client certificates. Declare the credential in one of these forms, and list it in the security of your wss server or of the channel's operations:

  • An API key in the query: a security scheme with type: httpApiKey and in: query.
  • A token sent as a subprotocol: a security scheme with type: httpApiKey, in: header, and name: Sec-WebSocket-Protocol. The browser sends subprotocols in the Sec-WebSocket-Protocol header.

The following example lets readers connect with either credential:

servers:
  production:
    host: live.cafe.example.com
    protocol: wss
    security:
      - $ref: '#/components/securitySchemes/queryKey'
      - $ref: '#/components/securitySchemes/subprotocolToken'
components:
  securitySchemes:
    queryKey:
      type: httpApiKey
      in: query
      name: api_key
    subprotocolToken:
      type: httpApiKey
      in: header
      name: Sec-WebSocket-Protocol

Give each message an example

Replay fills its message editor with the first example of the message the reader picks. Without an example, Replay generates a payload from the message schema. Add an example with a payload to each message that readers send, so they start from realistic values.

Hide the Try it button

Resources