A Flutter/Dart wrapper around centrifuge for connecting to Centrifugo over WebSocket: connection state, channel subscribe/publish, and emit with ACK (request/response on a channel).
Requirements: a Centrifugo-compatible backend that issues connection JWTs; the WebSocket URL is usually wss://<host>/connection/websocket.
In pubspec.yaml:
dependencies:
cf_socket_client: ^1.0.0import 'package:cf_socket_client/cf_socket_client.dart';
final socket = CentrifugalSocket(
config: CentrifugeSocketConfig(
url: 'wss://your-host/connection/websocket',
onSubscribe: (channel) async {
// Optional: call your HTTP API so the server authorizes the channel
},
),
);
socket.initCentrifugalClient();
socket.connectStatus.listen((status) {
// ConnectStatus: none | connecting | connected | disconnected
});
await socket.connect('<JWT_CONNECTION_TOKEN>');
socket.subscribe('channel_name', (data) {
// data: decoded JSON from the publication (often a Map)
});
await socket.emit('channel_name', {'text': 'hello'});
final reply = await socket.emitWithAck(
'channel_name',
{'action': 'ping'},
timeout: const Duration(seconds: 10),
);
await socket.disconnect();
socket.dispose();connect(token)— token must not be empty.subscribe— publications are JSON-decoded; the callback receivesdynamic(often aMap).emit— publishes JSON to the channel.emitWithAck— sends a payload including arequestId(UUID); your server should reply on the same channel to complete theFuture.
Use socket.client for the full centrifuge Client API.
cd example/cf_socket_client_example
flutter pub get
flutter runEnter the WebSocket URL, JWT, and channel name in the app; try Connect, Subscribe, Emit, and Emit + ACK. You need a real Centrifugo server and a valid token.
| Package | Notes |
|---|---|
| centrifuge | Centrifugo client protocol |
| uuid | requestId for emitWithAck |
This package is released under the MIT License.
It is a modified/forked version based on the original work by Centrifugal Labs LTD (MIT License).