A simple Swift client library for the Faye publish-subscribe messaging server. Faye is based on the Bayeux protocol and is compatible with CometD server.
GFayeSwift is implemented atop the Starscream Swift web socket library and will work on both Mac (pending Xcode 6 Swift update) and iPhone projects.
GFayeSwift is forked from FayeSwift to support Swift 4.2. Version 0.5.0 supports Swift 5.0.
FayeSwift was heavily inspired by the Objective-C client found here: FayeObjc
Note: For Swift 2.2 please use FayeSwift 0.2.0
Note: For Swift 3.2 please use FayeSwift 0.3.0
GFayeSwift is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "GFayeSwift"
Add GFayeSwift to dependencies:
dependencies: [
.package(url: "https://github.com/ckpwong/GFayeSwift.git", from: "0.5.1"),
],
targets: [
.target(
name: "My_Swift_Target",
dependencies: [
"GFayeSwift",
]
),
]
You can open a connection to your Faye/Bayeux/CometD server. Note that client
is probably best as a property, so your delegate can stick around. You can initiate a client with a subscription to a specific channel.
client = GFayeClient(aGFayeURLString: "ws://localhost:5222/faye", channel: "/cool")
client.delegate = self
client.connectToServer()
You can then also subscribe to additional channels either with block handlers like so:
let channelBlock:ChannelSubscriptionBlock = {(messageDict) -> Void in
let text: AnyObject? = messageDict["text"]
println("Here is the Block message: \(text)")
}
client.subscribeToChannel("/awesome", block: channelBlock)
or without them letting the delegate handle them like so:
self.client.subscribeToChannel("/delegates_still_rock")
After you are connected, there are some optional delegate methods that we can implement.
connectedToServer is called as soon as the client connects to the server.
func connectedToServer(client: GFayeClient) {
println("Connected to server")
}
connectionFailed is called when a cleint fails to connect to server either initially or on a retry.
func connectionFailed(client: GFayeClient) {
println("Failed to connect to server!")
}
disconnectedFromServer is called as soon as the client is disconnected from the server.
func disconnectedFromServer(client: GFayeClient) {
println("Disconnected from server")
}
didSubscribeToChannel is called when the subscribes to a channel.
func didSubscribeToChannel(client: GFayeClient, channel: String) {
println("subscribed to channel \(channel)")
}
didUnsubscribeFromChannel is called when the client unsubscribes to a channel.
func didUnsubscribeFromChannel(client: GFayeClient, channel: String) {
println("Unsubscribed from channel \(channel)")
}
The subscriptionFailedWithError method is called when the client fails to subscribe to a channel.
func subscriptionFailedWithError(client: GFayeClient, error: subscriptionError) {
println("SUBSCRIPTION FAILED!!!!")
}
The messageReceived is called when the client receives a message from any channel that it is subscribed to.
func messageReceived(client: GFayeClient, messageDict: NSDictionary, channel: String) {
let text: AnyObject? = messageDict["text"]
println("Here is the message: \(text)")
self.client.unsubscribeFromChannel(channel)
}
The delegate methods give you a simple way to handle data from the server, but how do you publish data to a channel?
You can call sendMessage to send a dictionary object to a channel
client.sendMessage(["text": textField.text], channel: "/cool")
There is a sample Faye server using the NodeJS Faye library. If you have NodeJS installed just run the following commands to install the package:
npm install
And then you can start the Faye server like so:
node faye_server.js
Check out the GFayeSwiftDemo project to see how to setup a simple connection to a Faye server.
GFayeSwift requires at least iOS 8/OSX 10.10 or above.
GFayeSwift is licensed under the MIT License.