Engine.IO server implementation in Swift, built on top of Hummingbird.
This package provides:
EngineIO: the server libraryEngineIOTestApp: a small echo server used for protocol testing
The project currently focuses on Engine.IO transport behavior, including:
- HTTP long-polling
- WebSocket upgrades
- Engine.IO v4 packet encoding and decoding
- Protocol test-suite compatibility via Docker
Add the package dependency to your Package.swift:
.package(url: "https://github.com/thomasauger/swift-server-engine.io.git", exact: "4.0.0-beta.2")Then depend on the EngineIO product:
.product(name: "EngineIO", package: "swift-server-engine.io")import EngineIO
@main
struct App {
static func main() async throws {
let server = Server(
port: 3000,
configuration: .init(
heartbeat: .init(
pingTimeout: .milliseconds(200),
pingInterval: .milliseconds(300)
),
transport: .init(maxPayload: 1_000_000),
lifecycle: .init(
onMessage: { connection, data in
await connection.send(data)
},
onError: { error in
print("Engine.IO error:", error.message)
}
)
)
)
try await server.run()
}
}The demo executable in Demo/EngineIO/App.swift uses the same setup.
If you want to embed Engine.IO into an existing Hummingbird app, use EngineIOEndpoint:
import EngineIO
import Hummingbird
import HummingbirdWebSocket
let endpoint = EngineIOEndpoint(configuration: .init())
let router = Router()
endpoint.install(into: router)
let app = Application(
router: router,
server: .http1WebSocketUpgrade(configuration: endpoint.webSocketConfiguration) { request, _, logger in
await endpoint.shouldUpgrade(request: request, logger: logger)
}
)Run the Swift test suite:
swift testRun the Engine.IO protocol tests:
docker compose run --build --rm engine-testRun both:
make test- Engine.IO protocol repository: https://github.com/socketio/engine.io-protocol
- Socket.IO website: https://socket.io/