Skip to content

Swift client for ShareDB (SwiftNIO + Combine). Realtime data sync with Operational Transform (OT).

License

Notifications You must be signed in to change notification settings

zeeyang/share-kit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Swift client for ShareDB (SwiftNIO + Combine). Realtime data sync with Operational Transform (OT).

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages