Skip to content

Latest commit

 

History

History
65 lines (59 loc) · 2.57 KB

README.md

File metadata and controls

65 lines (59 loc) · 2.57 KB

ShareKit
MIT License Swift 5.2 Continuous Integration

ShareKit

Realtime data sync for Swift using ShareDB. Idiomatically designed to work with Combine and SwiftUI.

Features

  • 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

Example Usage

Note: working knowledge of Combine is required.

Establish connection

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")
}

Subscribe to documents

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)
}