Separate port
Serving a gateway on its own port with ulo-ws-hyper, and choosing between that and the HTTP server's port.
A gateway is served by one of two servers, and its attribute says which. port = http is the default. port = own selects a standalone WebSocket server on a port of its own.
Each server serves only the gateways naming it. No gateway is served on both: that would expose a gateway on a port its author did not choose.
The HTTP port
A gateway without port is reached through the upgrade hand-off that WsModule registers. The HTTP server reads it before route matching on any request carrying Upgrade. It works with ulo-http-hyper, and with an embedding whose host supports upgrades (axum, poem, rocket and salvo; actix-web does not, see Embedding). examples/24-ws-rooms serves HTTP and a gateway on one PORT.
Because the app's HTTP server answers first, its drain and load shedding apply before the hand-off sees the request: an upgrade arriving after the drain began, or over the HTTP server's max_inflight, gets the HTTP server's 503.
An own port
ulo-ws-hyper is a standalone HTTP/1.1 server over hyper that answers each upgrade request with the same handshake decision and runs the same connection driver. examples/23-ws-gateway declares port = own and binds only this server:
use ulo_runtime_tokio::{Tokio, shutdown_signal};
use ulo_ws_hyper::Server;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let port = std::env::var("PORT").unwrap_or_else(|_| "3000".into());
let app = ex_23_ws_gateway::builder()
.runtime(Tokio::current())
.wire()?
.connect()
.await?
.bind(Server::new(format!("127.0.0.1:{port}")))
.listen()
.await?;
for bound in app.addresses() {
println!("listening on http://{}", bound.addr);
}
let shutdown = app.serve(shutdown_signal()).await?;
println!("closed on {}", shutdown.signal);
Ok(())
}A request off the gateway paths is answered 404. An HTTP server and an own-port WebSocket server bind side by side, each with its own endpoint: examples/39-one-app calls .bind(Http::new(..)).bind(Ws::new(..)) on one app.
One case listen refuses, and one it cannot see:
- A server with no
port = owngateway among its mounted handlers is refused, since it would serve nothing. - A
port = owngateway in an application that binds noulo_ws_hyper::Serveris reachable by nothing, and nothing reports it. Check that each declaredport = ownhas a server.
Rooms and broadcast are WsModule's. An application that binds ulo-ws-hyper without importing WsModule gets an in-memory adapter of the server's own, which no Dep<Rooms> reaches. Import WsModule::for_root() as well when handlers broadcast.
Server settings
ulo_ws_hyper::Server carries the connection-level settings of an HTTP server and the gateway defaults that WsModule carries for the HTTP port:
| Method | Effect |
|---|---|
Server::new(endpoint), .endpoint(..) | where to listen: text, SocketAddr or an inherited socket |
.tls(Tls) | wss, ALPN http/1.1. See TLS for loading. |
.header_timeout(Bound), .handshake_timeout(Bound) | as on the HTTP server: 30 seconds unset |
.max_message, .max_connections, .max_inflight, .max_outbound, .ping_interval, .pong_timeout | defaults for a gateway attribute that leaves the value unset |
.server_max_inflight(Count) | messages in flight across every connection of the server: 1,024 unset |
A zero limit is refused in prepare, with every other configuration failure of the server.
Runtimes
Server is ServerOn on tokio's listener, behind the default tokio feature. On smol, turn the feature off and name the listener: ulo_ws_hyper::ServerOn::<ulo_listener_smol::SmolListener>::new(..). See Runtimes.
Other stacks
ulo-ws depends on no runtime and owns no socket. A server on another stack is socket code around three calls of the crate's GatewayTable: own_port in prepare, handshake per upgrade request, and Switch::serve per admitted connection. ulo-ws-conformance runs one scenario list against every server through a Host trait the server's crate implements in its tests.