🇵🇰 🇯🇵
MeteorDDP A client for Meteor servers, written in Swift 5!
Following web sockets methods are used to connect MeteorDDP to Meteor Servers.
- Starscream Min iOS 8
- URLSessionWebSocketTask Min iOS 13
Table of Contents
- MeteorDDP
- Description
- Demonstration
- Requirements
- Installation
- CocoaPods
- Carthage
- Manual Installation
- Getting Started
- Contributions & License
MeteorDDP
Description
Inspired from SwiftDDP 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
is client for Meteor servers for iOS platform, which provides observers for all changes in events binded through a meteor server.
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:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'MeteorDDP'
end
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 and the global instance:
import MeteorDDP
let meteor: MeteorClient = {
MeteorClient(url: url, webSocket: .webSocketTask, requestTimeout: 15)
}()
// By default the preferred websocket method is starscream
##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 and create the global instance as mentioned below.
Getting Started
Following is the example to configure meteor in swift.
let meteor: MeteorClient = {
MeteorClient(url: url, webSocket: .starscream)
}()
// Global meteor subscriptions manager (optional to manage through this way)
let meteorCollection: MeteorCollections = {
MeteorCollections(client: meteor)
}()
public static var loggingLevel: Level = .normal
Connecting to a Meteor server
meteor.connect {
// do something after the client connects
}
Login using email and password.
meteor.loginWithPassword("[email protected]", password: "********") { (result, error) in
if error != nil {
//Login Failed
}
else {
// do something after login
}
}
Login using username and password.
meteor.loginWithUsername("Ali", password: "********") { (result, error) in
if error != nil {
//Login Failed
}
else {
// do something after login
}
}
Login anyway (either with valid email or username).
meteor.login("Ali", password: "********") { (result, error) in
if error != nil {
//Login Failed
}
else {
// do something after login
}
}
Log out.
meteor.logout { (result, error) in
if error != nil {
//Logout Failed
}
else {
// do something after logout
}
}
Disconnect MeteorDDP.
meteor.disconnect()
Subscribe
Subscribe and unsubscribe to a subset of a collection
meteor.subscribe("todos", [1,2,3,4]) {
// Do something when the todos subscription is ready
}
meteor.unsubscribe("SubID") {
// Unsubscribed of SubID
}
meteor.unsubscribe(withName: "todos") {
// Unsubscribed from todos
}
meteor.unsubscribeAll {
// Unsubscribed to all
}
Manage through subscription manager
// First subscribe with name
meteorCollection.subscribe("todos", params: nil) { events, document in
// Do something when the todos subscription is ready
}
// Listen to collection changes
meteorCollection.collectionDidChange = { collection in
// collection.documents new or changed dataset
}
// Add observer to some event, like dataAdded, dataChange or dataRemove
meteor.addEventObserver("todos", event: .dataAdded) {
guard let value = $0 as? MeteorDocument else {
return
}
// value.id is the unique ID of MeteorDocument
self.documents[value.id] = value.fields
}
// Optionally remove events registered against subscription
meteor.removeEventObservers("todos", event: [.dataAdded, .dataChange, .dataRemove])
// Unsubscribe
meteorCollection.unsubscribe("todos")
Update collection from client
// operation could be insert, remove or update against given meteor document
// keyValue is the update request to meteor server against that collection
meteor.updateColection("todos", type: .update, documents: [["_id": "MeteorDocumentID"],["$set": keyValue]]) { (res, error) in
if error == nil {
// Successfully updated
}
}
Call a method
meteor.call("tasks.insert", params: [textField.text!]) { (res, error) in
if error == nil && res != nil {
// do something with the response
}
}
Observers
Delegate
MeteorDelegate could receive the udpates in single method described below
meteor.delegate = self
func didReceive(name: MeteorEvents, event: Any) {
switch name {
case .method:
// event -> MeteorMethod object
case .websocket:
// event -> WebSocketEvent, could be connected, disconnected, text, error
case .dataAdded:
// event -> MeteorDocument object
case .dataChange:
// event -> MeteorDocument object
case .dataRemove:
// event -> MeteorDocument object
}
}
Callback
Simple add the observer with specified event anywhere in your code!
// Event could be any as mentioned in MeteorDelegate
meteor.addEventObserver("todos", event: .dataAdded) {
// $0 is the data against that event
}
MeteorEncodable
Below helper methods are available to encode or decode an object
MeteorEncodable.encode
MeteorEncodable.decode
MeteorOAuth
MeteorOAuthViewController
// MeteorLoginService are twitter, github, google, facebook
// MeteorOAuthViewController is to present MeteorOAuth
#Contributions & License
MeteorDDP
is available under the MIT license. See the LICENSE file for more info.
Pull requests are welcome! The best contributions will consist of substitutions or configurations for classes/methods known to block the main thread during a typical app lifecycle.
Follow and support us Meteor Pakistan on facebook
I would love to know if you are using MeteorDDP
in your app, send an email to Engr. Ahsan Ali