MeteorDDP
A client for Meteor servers, written in Swift 5!
MeteorDDP is really helpful to integrate servers written in meteor (a framework written in javascript) using native Swift in iOS.
Meteor is a Node-based full-stack framework which allows to create
reactive webapps, that could easily be ported to Android and iOS
platforms. Reactive webapp implies real-time behaviour: There is a
continuous connection between the client and server side, and so, a
change made in application by any means(direct entry in database, from
server, or even by a client) is reflected on every instance of the
application without any page reload.
Demonstration
MeteorDDP
A client for Meteor servers, written in Swift 5
To run the example project, clone the repo, and run pod install
from the Example directory first.
Requirements
-
iOS 8.0+
-
Xcode 11.0+
-
Swift 5+
Installation
MeteorDDP
can be installed using CocoaPods, Carthage, or manually.
CocoaPods
MeteorDDP
is available through CocoaPods. To install CocoaPods, run:
$ gem install cocoapods
Then create a Podfile with the following contents:
Finally, run the following command to install it:
$ pod install
Carthage
To install Carthage, run (using Homebrew):
$ brew update
$ brew install carthage
Then add the following line to your Cartfile:
github "EngrAhsanAli/MeteorDDP" "master"
Then import the library in all files where you use it:
Manual Installation
If you prefer not to use either of the above mentioned dependency managers, you can integrate MeteorDDP
into your project manually by adding the files contained in the Classes folder to your project.
Getting Started
Following is the example to configure meteor in swift.
Usage:
Setting basic configuration options
Connecting to a Meteor server
Login using email and password.
Login using username and password.
Log out.
The client also posts a notification when the user signs in and signs out, and during connection failure events.
Subscribe to a subset of a collection
Change the subscription's parameters and manage your subscription with unsubscribe
Call a method on the server
When passing parameters to a server method, the parameters object must be serializable with NSJSONSerialization
Simple in-memory persistence
SwiftDDP includes a class called MeteorCollection that provides simple, ephemeral dictionary backed persistence. MeteorCollection stores objects subclassed from MeteorDocument. Creating a collection is as simple as:
For client side insertions, updates and removals:
For each operation the action is executed on the client, and rolled back if the server returns an error.
Tips for CLI
If you make a command line tool, you need to call the function dispatchMain in main thread after proper DDP settings. Otherwise, you will encounter a dead lock.
Example: Creating an array based custom collection
The following pattern can be used to create custom collections backed by any datastore
In this example, we'll create a simple collection to hold a list of contacts. The first thing we'll do is create an object to represent a contact. This object has four properties and a method named update that maps the fields NSDictionary to the struct's properties. Update is called when an object is created and when an update is performed. Meteor will always transmit an id to identify the object that should be added, updated or removed, so objects that represent Meteor documents must always have an id field. Here we're sticking to the MongoDB convention of naming our id _id.
Next, we'll create the collection class that will hold our contacts and provide the logic to respond to server-side changes to the documents and the subscription set. SwiftDDP contains an abstract class called AbstractCollection that can be used to build custom collections. Subclassing AbstractCollection allows you to override three methods that are called in response to events on the server: documentWasAdded, documentWasChanged and documentWasRemoved.
So far, we're able to process documents that have been added, changed or removed on the server. But the UserCollection class still lacks the ability to make changes to both the local datastore and on the server. We'll change that. In the UserCollection class, create a method called insert.
The key parts of this method are:
- (1) save the new contact to the array we created in UserCollection
- (2) invoke client.insert to initiate an insert on the server
- (3) remove the contact from the local store if the server rejects the insert
Creating update and remove methods are also easy to create, and follow the same patern as insert. For a more extensive example of the patterns shown here, have a look at MeteorCollection.swift
MeteorCollection is an in-memory collection implementation suitable for simple applications.