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 1.0.1
Websocket upgrade support for Hummingbird
⭐️ 14
🕓 15 weeks ago
iOS tvOS linux macOS iOS
.package(url: "https://github.com/hummingbird-project/hummingbird-websocket.git", from: "1.0.1")

Hummingbird Websocket

Adds support for upgrading HTTP connections to WebSocket.

Usage

let app = HBApplication()
// add HTTP to WebSocket upgrade
app.ws.addUpgrade()
// add middleware to websocket initial requests
app.ws.add(middleware: HBLogRequestsMiddleware(.info))
// on websocket connect. 
app.ws.on("/ws") { req, ws in
    // send ping and wait for pong and repeat every 60 seconds
    ws.initiateAutoPing(interval: .seconds(60))
    // on read from client send the same data back
    ws.onRead { data, ws in
        ws.write(data)
    }
}
app.start()
app.wait()

Documentation

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

GitHub

link
Stars: 14
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

v1.0.1
15 weeks ago

Patch release changes

  • When adding websocket upgrade use new HBChannelInitializer.addProtocolUpgrade
  • Cancel auto ping task when websocket channel is closed
  • Ensure websocket channel is fully closed when other side closes input.
  • InitiateAutoPing schedules the first ping instead of sending it immediately
  • HBWebSocketClient sets up HTTPDecoder with leftOverBytesStrategy to .forwardBytes to ensure any remaining data in channel is passed onto websocket channel handler.

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