2016-04-04 10:52:43 +10:00
2016-01-06 17:58:50 +11:00
2016-04-04 10:52:43 +10:00

FayeSwift

Join the chat at https://gitter.im/hamin/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

Example

Installation

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() {
   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() {
   println("Failed to connect to Faye server!")
}

disconnectedFromServer

disconnectedFromServer is called as soon as the client is disconnected from the server..

func disconnectedFromServer() {
   println("Disconnected from Faye server")
}

didSubscribeToChannel

didSubscribeToChannel is called when the subscribes to a Faye channel.

func didSubscribeToChannel(channel: String) {
   println("subscribed to channel \(channel)")
}

didUnsubscribeFromChannel

didUnsubscribeFromChannel is called when the client unsubscribes to a Faye channel.

func didUnsubscribeFromChannel(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(error: String) {
   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(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")

Requirements

FayeSwift requires at least iOS8 / tvOS9.

TODOs

  • Cocoapods Integration
  • Complete Docs
  • Add Unit Tests
  • Rethink use of optionals (?)
  • Add block handlers (?)
  • Support for a long-polling transport (?)

License

BayeuxSwift is licensed under the MIT License and is a derivative of the FayeSwift project.

FayeSwift is licensed under the MIT License.

S
Description
Pure Swift Library for connecting to Bayeux-over-Websockets Clients, Such as Fanout and Faye.
Readme MIT 116 KiB
Languages
Swift 100%