rpc_dart_websocket
WebSocket caller/responder transports for rpc_dart, web-safe (dart2js and
Wasm) as well as VM.
RpcWebSocketCallerTransport— client transport;connect()awaitsWebSocketChannel.ready, andreconnect()re-attaches to a fresh socket while keepingincomingMessagesstable across the swap.RpcWebSocketResponderTransport— server-side transport for one accepted connection.RpcWebSocketServer— anIRpcServerthat accepts connections and wires an endpoint per client.RpcWebSocketChannel— the rawIRpcChannelbyte pipe, if you want to build the stack yourself viaRpcChannelTransport.fromChannel.
Multiplexing uses the core 9-byte channel frame (stream id + flags + length), so all four call kinds share one socket.
Usage
import 'package:rpc_dart_websocket/rpc_dart_websocket.dart';
final transport = await RpcWebSocketCallerTransport.connect(
Uri.parse('ws://localhost:8080'),
);
final caller = RpcCallerEndpoint(transport: transport);
Unlike rpc_dart_http2, this is not the gRPC wire protocol — it is the
rpc_dart frame protocol over WebSocket, so both peers must be rpc_dart.
Serving (VM)
package:rpc_dart_websocket/io.dart owns the HTTP-upgrade seam, which is where
every server-side decision has to be made:
final http = await HttpServer.bind(host, port);
final server = RpcWebSocketServer(
connections: rpcWebSocketConnections(
http,
pingInterval: const Duration(seconds: 30),
allowedOrigins: {'https://app.example.com'},
),
onEndpointCreated: (endpoint) => endpoint.registerServiceContract(MyApi()),
);
allowedOrigins— set this if browsers reach your server. WebSocket is not subject to the same-origin policy: any page can open a socket to your server and the browser attaches the user's cookies. CheckingOriginat the handshake is the only protocol-level defence. Requests with noOrigin(every non-browser client) are allowed; see the API docs for why.allowUpgrade— the general form, for a token in the query string, a header, or a path check. Synchronous, because it runs in the accept path.pingInterval— half-open detection. Without it a server keeps every dead connection, and its contracts, forever.compression— permessage-deflate, OFF by default because it is an unbounded decompression bomb from an unauthenticated peer.
Libraries
- io
- dart:io-only helpers for
rpc_dart_websocket. - rpc_dart_websocket
- Web-safe WebSocket transport for rpc_dart.