Swiftpack.co - hummingbird-project/hummingbird-websocket as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by hummingbird-project.
hummingbird-project/hummingbird-websocket 2.0.0-alpha.3
Websocket upgrade support for Hummingbird
⭐️ 21
🕓 Yesterday
iOS macOS tvOS linux macOS iOS
.package(url: "https://github.com/hummingbird-project/hummingbird-websocket.git", from: "2.0.0-alpha.3")

Hummingbird Websocket

Adds support for upgrading HTTP1 connections to WebSocket.

Usage

Setup WebSocket upgrades with a closure that either returns .upgrade with response headers and the handler for the WebSocket or a .dontUpgrade

let app = Application(
    router: router,
    server: .http1WebSocketUpgrade { request, channel, logger in
        // upgrade if request URI is "/ws"
        guard request.uri == "/ws" else { return .dontUpgrade }
        // The upgrade response includes the headers to include in the response and 
        // the WebSocket handler
        return .upgrade([:]) { inbound, outbound, context in
            for try await packet in inbound {
                // send "Received" for every packet we receive
                try await outbound.write(.text("Received"))
            }
        }
    }
)
app.runService()

Or alternatively use a Router. Using a router means you can add middleware to process the initial upgrade request before it is handled eg for authenticating the request.

let wsRouter = Router(context: BasicWebSocketRequestContext.self)
wsRouter.middlewares.add(BasicAuthenticator())
// An upgrade only occurs if a WebSocket path is matched
wsRouter.ws("/ws") { request, context in
    // allow upgrade
    .upgrade()
} onUpgrade: { inbound, outbound, context in
    for try await packet in inbound {
        // send "Received" for every packet we receive
        try await outbound.write(.text("Received"))
    }
}
let app = Application(
    router: router,
    server: .http1WebSocketUpgrade(webSocketRouter: wsRouter)
)
app.runService()

Documentation

You can find documentation for HummingbirdWebSocket here. The hummingbird-examples repository has a number of examples of different uses of the library.

GitHub

link
Stars: 21
Last commit: Yesterday
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

v2.0.0 Alpha 3
Yesterday

Major release changes

  • Add missing ServiceLifecycle dependency to HummingbirdWSClient. PR #56 from @tkrajacic
  • Return close code and reason from WebSocketClient.connect. PR #58

Minor changes

  • Require CompressNIO v1.2 with decompression fix
  • Add minimum size of frame before running per message-deflate compression configuration. PR #57
  • Add WebSocketOutboundWriter.close(_:reason:) to close WebSocket connection with custom close code and reason

Patch changes

  • Improve trace output of frames.
  • Fix bug where exiting a handler because you received a close frame would hang. PR #57

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics