PieSocket SDK for Flutter written in Dart.
Add PieSocket into your project.
flutter pub add piesocket_channels
Import the library
import 'package:piesocket_channels/channels.dart';Create a Channel instance as shown below.
Chanel channel = Channel.connect("wss://example.com", true)
channel.listen("system:message", (PieSocketEvent event) {
log("WebSocket message arrived!");
print(event.toString());
});Use following code to create a Channel with PieSocket's managed WebSocket servers.
Get your API key and Cluster ID here: Get API Key
PieSocketOptions options = PieSocketOptions();
options.setClusterId("demo");
options.setApiKey("VCXCEuvhGcBDP7XhiJJUDvR1e1D3eiVjgZ9VRiaV");
PieSocket piesocket = PieSocket(options);
Channel channel = piesocket.join("chat-room");Set version: "4" to share a single WebSocket across every join()
call. join() still returns a Channel synchronously, same as v3 — the
multiplexing happens in the background:
PieSocketOptions options = PieSocketOptions();
options.setClusterId("demo");
options.setApiKey("VCXCEuvhGcBDP7XhiJJUDvR1e1D3eiVjgZ9VRiaV");
options.setVersion("4");
PieSocket piesocket = PieSocket(options);
Channel chat = piesocket.join("chat-room"); // opens the socket
Channel alerts = piesocket.join("alerts"); // rides the same socket
chat.listen("message", (event) { /* ... */ });
alerts.publishEvent("ping", data: {"at": DateTime.now().toIso8601String()});Notes for v4:
join()stays synchronous even for a multiplexed channel — thesystem::subscribecontrol frame is sent in the background. Listen forsystem:connected(primary channel) the same way you would under v3. A secondary channel's subscribe failing (e.g.system::subscribe_error) is not delivered to the channel's own listeners — it's only logged — matching how the JS SDK handles this same case.- Presence is delta-based. The full roster arrives once; after that
Channel's member list is kept in sync from join/leave deltas. Callchannel.refreshMembers()to re-sync from the server on demand. - All v4 system events are double-colon (
system::member_joined,system::binary, etc.), unlike v3's single-colonsystem:events. - Binary needs no opt-in. Any binary frame arrives as a
system::binaryevent whosedatais a base64 string — decode it withbase64Decode. To send, callchannel.sendBinary(bytes)— a raw binary frame the server re-wraps assystem::binaryfor every other client (JS peers included). Primary channel only; a secondary channel'ssendBinarythrows. publishEvent(event, {data, meta})sends a structured payload (a Map, List, or primitive) directly, without needing to pre-jsonEncodeit into aPieSocketEventfirst — use this instead ofpublish(PieSocketEvent)for anything beyond a plain string, to avoid double-encoding it on the wire.notifySelfis connection-wide, not per-channel — if the firstjoin()call is the one that opens the shared socket, every other channel multiplexed onto it also gets that socket'snotifySelfsetting.- Unsubscribing the connect-time channel promotes another joined channel to keep the connection alive; a few in-flight frames may be missed during the swap.
- Guarded channels (
private-prefix, orforceAuth: true) resolve their JWT fromauthEndpointthe same as v3, withoutjoin()waiting on the fetch — ajoin()for another room that races in while that fetch is still in flight attaches once it resolves, rather than opening a second shared connection.
PieRTC is programmable WebRTC over v4 — the Flutter counterpart to
piesocket-js's PieRTC. There's no v3 equivalent in this SDK. It depends on
flutter_webrtc (already a
dependency of this package) — your app still needs to add that package's own
camera/microphone permission entries to its AndroidManifest.xml/
Info.plist; this package has no platform folders of its own to put them in.
options.setVersion("4");
PieSocket piesocket = PieSocket(options);
Channel room = piesocket.join(
"video-room",
video: true,
onLocalVideo: (stream, pieRTC) { /* attach to a renderer */ },
onParticipantJoined: (uuid, stream) { /* attach remote stream */ },
onParticipantLeft: (uuid) { /* remove remote stream */ },
);- Pass
video: true,audio: true, orpieRTC: truetojoin()to mark a room as PieRTC —room.pieRTCis attached once the room's connection resolves (may be afterjoin()already returned, same as everything else under v4). - Signalling uses its own
rtc::namespace (rtc::offer,rtc::answer,rtc::candidate, etc.) — a plain PieSocket relay, no server-side special-casing, so a Flutter and a JS/web client can share the same room. room.pieRTC.shareScreen()renegotiates a screen track onto every peer (alongside the camera);stopScreenShare()removes it. Both fireonScreenSharingStopped(with this client's own uuid) and publishrtc::stopped_screen; the SDK also stops if the user ends the share from the OS UI. Zero-config on web, desktop, macOS and iOS (iOS uses in-app ReplayKit capture). Android additionally needs the app to run a foreground service of typemediaProjectionwhile sharing — the simplest way is theflutter_backgroundpackage (FlutterBackground.enableBackgroundExecution()beforeshareScreen()), which ships the<service>its manifest needs.
PieSocket is scalable WebSocket API service with following features:
- Authentication
- Private Channels
- Presence Channels
- Publish messages with REST API
- Auto-scalability
- Webhooks
- Analytics
- Authentication
- Upto 60% cost savings
We highly recommend using PieSocket over self hosted WebSocket servers for production applications.
system:connected is the event fired when WebSocket connection is ready, get a full list system messages here: PieSocket System Messages
For usage examples and more information, refer to: Official SDK docs