NIOWebSocketServer

public final class NIOWebSocketServer : WebSocketServer, Service

Vapor’s default WebSocketServer implementation. Includes conformance to WebSocketServer that is backed by a TrieRouter for registering multiple different websocket handlers.

A WebSocketServer determines whether HTTP requests requesting upgrade to the websocket protocol should be approved or denied. If approved, additional headers can be returned in the 101 switching protocols response.

When HTTP upgrade requests are approved, the WebSocketServer will handle the newly connected websocket clients.

HTTP upgrade requests will be handled by the WebSocketServer before invoking Vapor’s normal HTTP request pipeline (including middleware). Should an HTTP upgrade request be accepted, no other parts of Vapor’s pipeline will be invoked. Should the HTTP upgrade request be denied, the request will continue through Vapor’s HTTP pipeline normally.

Note: The WebSocketServer always runs behind an HTTP server and will only be invoked when HTTP requests request an upgrade.

  • All websocket responder routes that have been added to this NIOWebSocketServer.

    Declaration

    Swift

    public var routes: [Route<WebSocketResponder>]
  • Creates a new NIOWebSocketServer with default settings.

    Declaration

    Swift

    public static func `default`() -> NIOWebSocketServer
  • Registers a new Route<WebSocketResponder> to this NIOWebSocketServer.

    This is normally done using the convenience .get(...) methods. However, this method is useful for registering custom routes.

    Declaration

    Swift

    public func register(route: Route<WebSocketResponder>)

    Parameters

    route

    The websocket responder route to add to this websocket server.

  • Determines whether the HTTP request should be upgraded or not. Only HTTP requests that have requested websocket protocol upgrade will be supplied to this method.

    Declaration

    Swift

    public func webSocketShouldUpgrade(for request: Request) -> HTTPHeaders?

    Parameters

    request

    The HTTP request requesting upgrade to websocket protocol.

    Return Value

    HTTPHeaders to include in the 101 switching protocols HTTP response. If nil, the HTTP upgrade request will be denied.

  • Handles newly connected websocket clients. This method will only be called if webSocketShouldUpgrade(for:) returned non-nil HTTP headers.

    Declaration

    Swift

    public func webSocketOnUpgrade(_ webSocket: WebSocket, for request: Request)

    Parameters

    webSocket

    The newly connected websocket client. Use this to send and receive messages from the client.

    request

    The HTTP request that initiated the websocket protocol upgrade.

  • Registers a new websocket handling route at the supplied dynamic path.

    Declaration

    Swift

    @discardableResult
    public func get(at path: [PathComponent], use closure: @escaping (WebSocket, Request) throws -> ()) -> Route<WebSocketResponder>

    Parameters

    path

    Dynamic path to associate with this websocket upgrade closure. HTTP upgrade requests that contain a matching path will invoke the supplied on upgrade closure when the websocket client connects. Any parameterized values can be retrieved from the HTTP request supplied to the closure.

    closure

    Websocket on upgrade closure. Accepts newly upgraded websocket connections.

    Return Value

    Discardable websocket responder route. Use this route reference to append metadata to the route.

  • Registers a new websocket handling route at the supplied dynamic path.

    Declaration

    Swift

    @discardableResult
    public func get(_ path: PathComponent..., use closure: @escaping (WebSocket, Request) throws -> ()) -> Route<WebSocketResponder>

    Parameters

    path

    Dynamic path to associate with this websocket upgrade closure. HTTP upgrade requests that contain a matching path will invoke the supplied on upgrade closure when the websocket client connects. Any parameterized values can be retrieved from the HTTP request supplied to the closure.

    closure

    Websocket on upgrade closure. Accepts newly upgraded websocket connections.

    Return Value

    Discardable websocket responder route. Use this route reference to append metadata to the route.

  • Registers a new websocket handling route at the supplied dynamic path.

    Declaration

    Swift

    @discardableResult
    public func get(_ path: PathComponentsRepresentable..., use closure: @escaping (WebSocket, Request) throws -> ()) -> Route<WebSocketResponder>

    Parameters

    path

    Dynamic path to associate with this websocket upgrade closure. HTTP upgrade requests that contain a matching path will invoke the supplied on upgrade closure when the websocket client connects. Any parameterized values can be retrieved from the HTTP request supplied to the closure.

    closure

    Websocket on upgrade closure. Accepts newly upgraded websocket connections.

    Return Value

    Discardable websocket responder route. Use this route reference to append metadata to the route.