• Stars
    star
    157
  • Rank 238,399 (Top 5 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 3 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Real-time Apps the SwiftUI way

Sync

Swift Package Manager Twitter: @nerdsupremacist

Sync

Sync is a proof of concept for expanding on the Magic of ObservableObject, and making it work over the network. This let's you create real-time Apps in which your Observable Objects are in sync across multiple devices. This has a lot of applications just as:

  • IoT Apps
  • Multi-Player Mini Games
  • etc.

As of right now Sync works out of the box using WebSockets, however, it's not limited to web sockets and it allows for multiple kinds of connections. Some possible connections could be:

  • Bluetooth
  • Multi-Peer
  • MQTT
  • etc.

The sky is the limit!

Warning: This is only a proof of concept that I'm using for experimentation. I assume there's lots and lots of bugs in there...

Installation

Swift Package Manager

You can install Sync via Swift Package Manager by adding the following line to your Package.swift:

import PackageDescription

let package = Package(
    [...]
    dependencies: [
        .package(url: "https://github.com/nerdsupremacist/Sync.git", from: "1.0.0")
    ]
)

Usage

If you have ever used Observable Object, then Sync will be extremely easy to use. For this example we will create an app with a Switch that everyone can flip on or off as they like. We will build this using SwiftUI, WebSockets and a Vapor Server. Final code available here.

Sync

For this we will need a few additional packages:

Let's start by building our shared ViewModel. This is easy, instead of using ObservableObject we use SyncableObject. And instead of Published we use Synced:

class ViewModel: SyncableObject {
    @Synced
    var toggle: Bool = false

    init() { }
}

This ViewModel needs to be both on your App codebase as well as on the Server codebase. I recommend putting it in a shared Swift Package, if you're feeling fancy.

Next stop is to create our server. In this example every client will be using the exact same ViewModel. So we're creating a Vapor application, and using syncObjectOverWebSocket to provide the object:

import Vapor
import SyncWebSocketVapor

let app = Application(try .detect())

let viewModel = ViewModel()
app.syncObjectOverWebSocket("view_model") { _ in
    return viewModel
}

try app.run()

For our SwiftUI App, we need to use two things:

  • @SyncedObject: Like ObservedObject, but for Syncable Objects. It's a property wrapper that will dynamically tell SwiftUI when to update the UI
  • Sync: A little wrapper view to start the remote session

Our actual view then uses SyncedObservedObject with our ViewModel

struct ContentView: View {
    @SyncedObject
    var viewModel: ViewModel

    var body: some View {
        Toggle("A toggle", isOn: $viewModel.toggle)
            .animation(.easeIn, value: viewModel.toggle)
            .padding(64)
    }
}

And in order to display it we use Sync, and pass along the Web Socket Connection:

struct RootView: View {
    var body: some View {
        Sync(ViewModel.self, using: .webSocket(url: url)) { viewModel in
            ContentView(viewModel: viewModel)
        }
    }
}

Developing for Web?

No problem. You can scale this solution to the web using Tokamak, and use the same UI on the Web thanks to Web Assembly. Here are the Web Assembly specific packages for Sync:

Here's a small demo of that working:

Sync

Contributions

Contributions are welcome and encouraged!

License

Sync is available under the MIT license. See the LICENSE file for more info.

More Repositories

1

FancyScrollView

A SwiftUI ScrollView Designed to imitate the App Store and Apple Music ScrollViews (with or without a Parallax Header)
Swift
971
star
2

Snap

A customizable Snapping Drawer à la Apple Maps.

 100% in SwiftUI.
Swift
717
star
3

Graphaello

A Tool for Writing Declarative, Type-Safe and Data-Driven Applications in SwiftUI using GraphQL
Swift
493
star
4

Syntax

Write value-driven parsers quickly in Swift with an intuitive SwiftUI-like DSL
Swift
146
star
5

GraphZahl

A Framework to implement Declarative, Type-Safe GraphQL Server APIs using Runtime Magic 🎩
Swift
143
star
6

CovidUI

A simple App to Track the status of Covid-19 around the World. Using SwiftUI and GraphQL
Swift
78
star
7

memes

An online Multi-Player Meme Party Game written in Swift
Swift
70
star
8

Valid

Input Validation Done Right. A Swift DSL for Validating User Input using Allow/Deny Rules
Swift
37
star
9

CovidQL

GraphQL API for querying data related to Covid-19
Swift
31
star
10

StopTouchingYourFace

SwiftUI App that alerts you when you have touched your face
Swift
25
star
11

tmdb

A GraphQL Wrapper for The Movie Database
Swift
24
star
12

Sweeft

Swift but a bit Sweeter - More Syntactic Sugar for Swift #MakeSwiftGreatAgain
Swift
21
star
13

AssociatedTypeRequirementsKit

A Swift µFramework for dealing with the classic "Self or associated type requirements" errors
Swift
20
star
14

Fuzzi

Locally searching in Swift made simple (and fuzzily)
Swift
19
star
15

git-yolo

YOLO Mode for GIT
JavaScript
16
star
16

VariadicViewBuilder

Custom View Builder that allows you to build custom layouts
Swift
15
star
17

trumpify

Make your words be the BEST WORDS - Instant text trumpifier
JavaScript
14
star
18

VideoPlayer

µFramework containing a SwiftUI Video Player allowing for custom controls
Swift
13
star
19

Protected

Experimental API for Reads and Writes protected via Phantom types
Swift
11
star
20

LlamaLang

Repository for the Llama Programming Language. Work In Progress
Python
11
star
21

syntax-highlight-publish-plugin

Plugin to add syntax highlighting (for multiple languages) to your Publish Site, with the least amount of effort.
Swift
7
star
22

graphzahl-todo-app-example

Example Todo App Showcasing how to use GraphZahl with Vapor and Fluent
Swift
7
star
23

SyncExampleApp

Example App on how to use Sync to keep objects in sync
Swift
7
star
24

tmdb-relay-test

Test Project showcasing Relay + Typescript + GitHub Actions + Chakra
TypeScript
6
star
25

SyncTokamak

Swift
6
star
26

graphzahl-vapor-support

A set of extensions that allow to use GraphZahl with Vapor
Swift
6
star
27

GridView

A SwiftUI View that allows you to Render items in a Grid
Swift
5
star
28

Mealy

Swift Framework for Model-Based Testing using Mealy Machines
Swift
5
star
29

stalky-app

Always remember people's names
Swift
5
star
30

CodableEnum

µFramework that allows you to make an Enum with associated values conform to Codable
Swift
5
star
31

SyncWebSocketVapor

Web Socket Server Support for Sync
Swift
4
star
32

Pushie

Push Down Automata Creation in Swift. The easy way.
Swift
4
star
33

git-fuck

NPM Module for when you've committed to the wrong branch
JavaScript
4
star
34

graphaello-music-example

Example App using Graphaello
Swift
4
star
35

Assert

Swift UI Like DSL for Building Tests
Swift
4
star
36

MarkdownSyntax

Markdown Parser using Syntax
Swift
4
star
37

graphzahl-fluent-support

A set of extensions that allow to use Fluent APIs with GraphZahl
Swift
4
star
38

fresh-mac-setup

How to set up a fresh Mac the way I like it
Ruby
3
star
39

SyntaxTree

Model for building Syntax Trees
Swift
3
star
40

graphql-syntax

GraphQL Parser written in Syntax
Swift
3
star
41

graphaello-countries-example

Example App using Graphaello
Swift
3
star
42

GraphDSL

Script that given a GraphQL Server will create a Kotlin DSL for writing queries
Kotlin
2
star
43

graphzahl-vs-graphiti

Comparison of the same schema implemented with GraphZahl and with Graphiti as a comparison
Swift
1
star
44

SyncWebSocketWebAssemblyClient

Swift
1
star
45

ContextKit

A Public Basic API for providing a Compositable Type Safe Dict
Swift
1
star
46

SyncWebSocketClient

Web Socket Client support for Sync
Swift
1
star
47

syntax-highlight

Syntax Highlighting Extensions to the Syntax Framework
Swift
1
star
48

kotlin-worldbank-type-provider

Type Provider for the WorldBank API inside of Kotlin Scripting
Kotlin
1
star
49

kotlin-type-provider-template

Template for building Type Providers for Kotlin Scripting
Kotlin
1
star
50

TextMate

Support for parsing Text Mate Languages
Swift
1
star
51

StarWarsArray

A Swift Sequence indexed like Star Wars Movies
Swift
1
star
52

isAdnanGonnaBeLate

A simple Web Page with the Status of whether or not Adnan Will be Late
HTML
1
star