Realtime data sync for Swift using ShareDB. Idiomatically designed to work with Combine and SwiftUI.
- Memory and bandwidth efficient data synchronization using Operational Transform (OT)
- Modern Swift API specifically designed for Combine and SwiftUI
- Battle-tested, MIT licensed ShareDB server that can scale with any project
Note: working knowledge of Combine is required.
ShareKit uses Apple official SwiftNIO framework for Websocket connections. ShareConnection
is ShareKit's abstraction of the Websocket connection, which manages automatic re-connections and threading using EventLoopGroup
. To connect to a ShareDB server instance, simply pass the endpoint URL and closure for connection callback.
ShareClient(eventLoopGroupProvider: .createNew).connect("ws://localhost:8080") { connection in
print("Connected to ShareDB")
}
ShareDB document is composed of an unique ID, incremental version number, and a data payload with schemaless JSON. To subscribe to a document, first define a Codable
struct to decode the document data entity.
struct Player: Codable {
var name: String = ""
var score: Int = 0
}
Use connection.subscribe(...)
to send document subscription request.
ShareClient(eventLoopGroupProvider: .createNew).connect("ws://localhost:8080") { connection in
let document: ShareDocument<Player> = connection.subscribe("doc1", in: "collection")
}
ShareDocument
uses Combine publisher, ShareDocument.$data
, to broadcast document updates.
ShareClient(eventLoopGroupProvider: .createNew).connect("ws://localhost:8080") { connection in
let document: ShareDocument<Player> = connection.subscribe("doc1", in: "collection")
document.$data
.compactMap { $0 }
.receive(on: RunLoop.main)
.sink { player in
print(player)
}
.store(in: &bag)
}