FayeSwift
FayeSwift copied to clipboard
Swift Client Library for the Faye Pub-Sub messaging server (http://faye.jcoglan.com/)
FayeSwift
A simple Swift client library for the Faye publish-subscribe messaging server. FayeObjC is implemented atop the Starscream Swift web socket library and will work on both Mac (pending Xcode 6 Swift update) and iPhone projects.
It was heavily inspired by the Objective-C client found here: FayeObjc
Note: For Swift 2.2 please use FayeSwift 0.2.0
Example
Installation
FayeSwift is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "FayeSwift"
Swift Package Manager compatability is coming sson
Initializing Client
You can open a connection to your faye 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 = FayeClient(aFayeURLString: "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
connectedToServer is called as soon as the client connects to the Faye server.
func connectedToServer(client: FayeClient) {
println("Connected to Faye server")
}
connectionFailed
connectionFailed is called when a cleint fails to connect to Faye server either initially or on a retry.
func connectionFailed(client: FayeClient) {
println("Failed to connect to Faye server!")
}
disconnectedFromServer
disconnectedFromServer is called as soon as the client is disconnected from the server..
func disconnectedFromServer(client: FayeClient) {
println("Disconnected from Faye server")
}
didSubscribeToChannel
didSubscribeToChannel is called when the subscribes to a Faye channel.
func didSubscribeToChannel(client: FayeClient, channel: String) {
println("subscribed to channel \(channel)")
}
didUnsubscribeFromChannel
didUnsubscribeFromChannel is called when the client unsubscribes to a Faye channel.
func didUnsubscribeFromChannel(client: FayeClient, channel: String) {
println("UNsubscribed from channel \(channel)")
}
subscriptionFailedWithError
The subscriptionFailedWithError method is called when the client fails to subscribe to a Faye channel.
func subscriptionFailedWithError(client: FayeClient, error: subscriptionError) {
println("SUBSCRIPTION FAILED!!!!")
}
messageReceived
The messageReceived is called when the client receives a message from any Faye channel that it is subscribed to.
func messageReceived(client: FayeClient, 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 Faye channel?
sendMessage
You can call sendMessage to send a dictionary object to a channel
client.sendMessage(["text": textField.text], channel: "/cool")
Example Server
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
Example Project
Check out the FayeSwiftDemo project to see how to setup a simple connection to a Faye server.
Requirements
FayeSwift requires at least iOS 7/OSX 10.10 or above.
TODOs
- [x] Cocoapods Integration
- [x] Add block handlers
- [x] Complete Docs
- [ ] Add Unit Tests
- [ ] Swift Package Mananger Support
- [ ] Rethink use of optionals (?)
- [ ] Support for a long-polling transport (?)
License
FayeSwift is licensed under the MIT License.