PieSocket Realtime SDK for WebSockets written in Swift. Supports cross-platform Xcode projects for: iOS, iPad, Mac, etc.
Simply import this github repository into your Xcode project.
- In your Xcode project, go to File > Add packages
- Enter
https://github.com/piesocket/piesocket-swiftin the search box - Click "Add package"
Use following code to create a Channel with Managed PieSocket Server.
Get your API key and Cluster ID here: Get API Key
let options: PieSocketOptions = PieSocketOptions();
options.setClusterId(clusterId: "demo");
options.setApiKey(apiKey: "VCXCEuvhGcBDP7XhiJJUDvR1e1D3eiVjgZ9VRiaV");
let piesocket: PieSocket = PieSocket(pieSocketOptions: options);
let channel: Channel = piesocket.join(roomId: "chat-room");Use the following code to create a Channel with a self-hosted PieSocket Server.
let options: PieSocketOptions = PieSocketOptions();
options.setClusterDomain(clusterDomain: "localhost:4001");
options.setSsl(ssl: false);
let piesocket: PieSocket = PieSocket(pieSocketOptions: options);
let channel: Channel = piesocket.join(roomId: "chat-room");PieSocket Realtime 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
Set options.setVersion(version: "4") to share a single WebSocket across
every join() call. join() still returns a Channel synchronously.
let options = PieSocketOptions()
options.setClusterId(clusterId: "demo")
options.setApiKey(apiKey: "VCXCEuvhGcBDP7XhiJJUDvR1e1D3eiVjgZ9VRiaV")
options.setVersion(version: "4")
let piesocket = PieSocket(pieSocketOptions: options)
let chat = piesocket.join(roomId: "chat-room") // opens the socket
let alerts = piesocket.join(roomId: "alerts") // rides the same socket
chat.listen(eventName: "message") { event in /* ... */ }
alerts.publishEvent("ping", data: ["at": Date().timeIntervalSince1970])join()stays synchronous. Listen forsystem:connectedon the primary channel (the firstjoin()); secondary channels' subscribe acks are handled internally.- Delta presence.
channel.refreshMembers { members in ... }re-syncs. - All v4 system events are double-colon (
system::member_joined, …). publishEvent(_:data:meta:)sends a structured payload directly.- Binary. Inbound binary arrives as a
system::binaryevent whosedatais a base64 string.try channel.sendBinary(Data)sends a raw binary frame (primary channel only) that the server re-wraps assystem::binaryfor every other client — JS / Flutter / Android peers included.
Built on stasel/WebRTC. Signalling rides
its own rtc:: namespace — a plain relay, so Apple, Android and JS/web clients
interoperate in one room.
let rtc = PieRTCOptions()
rtc.video = true
rtc.audio = true
rtc.onLocalVideo = { stream, pieRTC in /* stream.videoTracks.first?.add(renderer) */ }
rtc.onParticipantJoined = { uuid, stream in /* attach remote stream */ }
rtc.onParticipantLeft = { uuid in /* remove renderer */ }
let room = piesocket.join(roomId: "video-room", pieRTC: rtc)- Add
NSCameraUsageDescription/NSMicrophoneUsageDescriptionto Info.plist. WebRTCis re-exported fromimport Channels— useRTCMTLVideoViewetc.pieRTC.shareScreen()(iOS) — one call, no extension target. In-app ReplayKit capture;pieRTC.stopScreenShare()stops.pieRTC.dispose()/piesocket.leave(roomId:)releases resources.
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