A Swift Package for Syncs Real-Time Web Applications
syncs-swift is Swift framework to work with Syncs.
syncs-swift is easy to setup. Syncs is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Syncs"
then run pod install
command
To run the example project, clone the repo, and run pod install
from the Example directory first.
Example project needs syncs server which is avilable here
Developers can create real-time connection by creating an instance of Syncs
class.
import Syncs
let io:Syncs=Syncs("ws://localhost:8080/syncs");
The path parameter is required that determines Syncs server address. initializer method has other input parameter:
autoConnect:boolean
: IfautoConnect
isfalse
then the Syncs instance will not connect to server on creation. To connect manuly to server developers should callio.connect()
method. default value istrue
.autoReconnect:boolean
: This config makes the connection presistent on connection drop. default value istrue
.reconnectDelay: number
: time to wait befor each reconnecting try. default value is1000
.debug:bolean
: This parameter enables debug mode on client side. default value isfalse
.
let io=Syncs("ws://localhost:8080/syncs", autoConnect: false, autoReconnect: true, reconnectDelay: 10000, debug: true)
Syncs client script can automatically connect to Syncs server. If autoConnect
config is set to false
, the developer should connect manual to server using connect
method.
Using connect
method developers can connect to defined server.
io.connect();
Target application can establish connection or disconnect from server using provided methods.
io.disconnect()
Developers can handle disconnect and close event with onDisconnect
and onClose
method.
io.onOpen {
// handle open
}
io.onClose {
// handle open
}
io.onDisconnect {
// handle open
}
Developers can use one listener to handle connection status
class ViewController: UIViewController,SyncsDelegate {
...
func onOpen(from syncs: Syncs) {
// handle open
}
}
It's also possible to check connection status using online
property of Syncs instance.
if(io.online){
//do semething
}
Syncs provides four abstraction layer over its real-time functionality for developers.
Developers can send messages using send
method of Syncs
instance to send JSON
message to the server.Also all incoming messages are catchable using onMessage
.
Syncs uses SwiftyJSON library to handle JSON messages.
io.send(data,JSON["message":"Hello, Syncs"]);
io.onMessage(){ data in
// handle message here
}
Or use SyncsDelegate
protocol
class ViewController: UIViewController,SyncsDelegate {
...
func onMessage(with data: JSON, from syncs: Syncs) {
// handle message here
}
}
With a Publish and Subscribe solution developers normally subscribe to data using a string identifier. This is normally called a Channel, Topic or Subject.
io.publish("mouse-move-event",jsonData);
io.subscribe("weather-update"){event,data,io in
}
Syncs provides Shared Data functionality in form of variable sharing. Shared variables can be accessible in tree level: Global Level, Group Level and Client Level. Only Client Level shared data can be write able with client.
To get Client Level shared object use shared
method of Syncs
instance.
let info:SharedObject=io.shared("info");
info.set("title","Syncs is cool!");
To get Group Level shared object use groupShared
method of Syncs
instance. First parameter is group name and second one is shared object name.
info:SharedObject=io.groupShared('vips','info');
info.getInteger("onlineVips")+" vip member are online";
To get Global Level shared object use globalShared
method of Syncs
instance.
let settings=io.globalShared("settings");
applyBackground(settings.getString("backgrounColor"));
It's possible to watch changes in shared object by using shared object as a function.
info.onChange { (values, by) in
}
The callback function has two argument.
values
: an array that contains names of changed properties.by:Enum
an Enum variable with two value ('SERVER'
and'CLIENT'
) which shows who changed these properties.
With help of RMI developers can call and pass argument to remote function and make it easy to develop robust and web developed application. RMI may abstract things away too much and developers might forget that they are making calls over the wire.
Before calling remote method from server ,developer should declare the function on client script.
functions
method in Syncs
instance is the place to declare functions.
io.functions("showMessage"){ args,promise in
//show message
return nil
}
To call remote method on server use remote
object.
io.remote("setLocation", args: latitude,longitude)
The remote side can return a result (direct value or Promise object) which is accessible using Promise
object provided by functions
.
io.functions("askUser"){ args,promise in
// ask user and return result
return result;
}
io.functions("startQuiz"){ args,promise in
// start quiz
return promise;
// call promise.result(...) later
}
// after a while
promise.result(quizStatics);
io.remote("getWeather", args: cityName){ result,error in
}