Class WebSocketServer

Class Documentation

class WebSocketServer

A WebSocket server for visualization in Foxglove.

After your server is started, you can open the Foxglove app to visualize your data. See Connecting to data.

Note

WebSocketServer is fully thread-safe, but WebSocketServerCallbacks may be invoked concurrently from multiple threads, so you will need to use synchronization in your callbacks.

Public Functions

uint16_t port() const

Get the port on which the server is listening.

FoxgloveError stop()

Gracefully shut down the websocket server.

void broadcastTime(uint64_t timestamp_nanos) const noexcept

Publishes the current server timestamp to all clients.

Requires the capability WebSocketServerCapabilities::Time.

Parameters:

timestamp_nanos – An epoch offset in nanoseconds.

FoxgloveError clearSession(std::optional<std::string_view> session_id = std::nullopt) const noexcept

Sets a new session ID and notifies all clients, causing them to reset their state.

If session_id is not provided, generates a new one based on the current timestamp.

Parameters:

session_id – Optional session ID.

FoxgloveError addService(Service &&service) const noexcept

Advertises support for the provided service.

This service will be available for clients to use until it is removed with removeService(), or the server is stopped.

This method will fail for various reasons, with the following error codes:

  • DuplicateService: A service with the same name is already registered.

  • MissingRequestedEncoding: The service didn’t declare a request encoding, and the server was not configured with a global list of supported encodings.

  • ServicesNotSupported: The server was not convfigured with the Services capability.

Parameters:

service – The service to add.

FoxgloveError removeService(std::string_view name) const noexcept

Removes a service that was previously advertised.

This method will fail with FoxgloveError::Utf8Error if the service name is not a valid UTF-8 string.

Parameters:

name – The name of the service to remove.

void publishParameterValues(std::vector<Parameter> &&params)

Publishes parameter values to all subscribed clients.

Requires the capability WebSocketServerCapabilities::Parameters.

Parameters:

params – Updated parameters.

void publishConnectionGraph(ConnectionGraph &graph)

Publish a connection graph to all subscribed clients.

This requires the capability WebSocketServerCapabilities::ConnectionGraph

Parameters:

graph – The connection graph to publish.

FoxgloveError publishStatus(WebSocketServerStatusLevel level, std::string_view message, std::optional<std::string_view> id = std::nullopt) const noexcept

Publishes a status message to all clients.

The server may send this message at any time. Client developers may use it for debugging purposes, display it to the end user, or ignore it.

The caller may optionally provide a message ID, which can be used in a subsequent call to removeStatus().

Parameters:
  • level – Status level value.

  • message – Status message.

  • id – Optional message ID.

FoxgloveError removeStatus(const std::vector<std::string_view> &ids) const

Removes status messages from all clients.

Previously published status messages are referenced by ID.

Parameters:

ids – Message IDs.

Public Static Functions

static FoxgloveResult<WebSocketServer> create(WebSocketServerOptions &&options)

Create a new WebSocket server with the given options.