License: MIT, Platform: iOS, Carthage: compatible
Language: Swift 5
If you are wondering "what is STOMP??" - see the Apache ActiveMQ website for an introduction to message brokers, MQTT, Websockets and STOMP.
StompClientLib is a STOMP client in Swift. It uses Facebook's SocketRocket as a websocket dependency. SocketRocket is written in Objective-C but StompClientLib's STOMP part is written in Swift and its usage is Swift. You can use this library in your Swift 5+ projects.
This is a fork from StompClientLib, forked in February 6th 2020 by michaelpeternell. StompClientLib is a fork from AKStompClient.
- Carthage works now properly (I added a Cartfile that specifies dependencies)
- Proper configurable logging instead of just printing to the console.
- Added documentation about how to login with username and password.
To run the example project, clone the repo and set a development team for code signing (or run in the simulator). (Node by michaelpeternell: I didn't manage to make the example actually work, but I could compile it and run it.)
- iOS 8.0+
- XCode 11
- Swift 5
StompClientLib is available through Carthage.
Add the following line to your Cartfile:
github "michaelpeternell/StompClientLib" ~> 1.4
import StompClientLib
Once imported, you can open a connection to your WebSocket server.
var socketClient = StompClientLib()
let url = NSURL(string: "wss://your-url.example.com:1337")!
socketClient.openSocketWithURLRequest(request: NSURLRequest(url: url as URL), delegate: self, connectionHeaders: ["login": "kermit", "passcode": "Miss Piggy"])
You may provide username and password by specifying the connectionHeaders
"login" and "passcode". If you don't need authentication, just omit that parameter. You should use "ws" or "wss" as a protocol.
After you are connected, StompClientLib will call some delegate methods, which you will need to implement.
func stompClientDidConnect(client: StompClientLib) {
print("Socket is connected")
// Stomp subscribe will be here!
socketClient.subscribe(destination: topic)
// Note : topic needs to be a String object
}
func stompClientDidDisconnect(client: StompClientLib) {
print("Socket is Disconnected")
}
Your json message will be converted to JSON Body as AnyObject and you will receive your message in this function
func stompClient(client: StompClientLib, didReceiveMessageWithJSONBody jsonBody: AnyObject?, akaStringBody stringBody: String?, withHeader header: [String : String]?, withDestination destination: String) {
print("Destination : \(destination)")
print("JSON Body : \(String(describing: jsonBody))")
print("String Body : \(stringBody ?? "nil")")
}
Your json message will be converted to JSON Body as AnyObject and you will receive your message in this function
func stompClientJSONBody(client: StompClientLib, didReceiveMessageWithJSONBody jsonBody: String?, withHeader header: [String : String]?, withDestination destination: String) {
print("DESTIONATION : \(destination)")
print("String JSON BODY : \(String(describing: jsonBody))")
}
If you will use STOMP for in-app purchase, you might need to use this function to get receipt
func serverDidSendReceipt(client: StompClientLib, withReceiptId receiptId: String) {
print("Receipt : \(receiptId)")
}
Your error message will be received in this function
func serverDidSendError(client: StompClientLib, withErrorMessage description: String, detailedErrorMessage message: String?) {
print("Error Send : \(String(describing: message))")
}
If you need to control your server's ping, here is your part
func serverDidSendPing() {
print("Server ping")
}
There are functions for subscribing and unsubscribing. Note : You should handle your subscribe and unsubscibe methods ! Suggestion : Subscribe to your topic in "stompClientDidConnect" function and unsubcribe to your topic in stompClientWillDisconnect method.
let topic = "/topic/FOO.BAR.1337"
socketClient.subscribe(destination: topic)
socketClient.unsubscribe(destination: topic)
Important: You have to send your destination for both subscribe or unsubscribe!
let destination = "/topic/your_topic"
let ack = destination
let id = destination
let header = ["destination": destination, "ack": ack, "id": id]
// subscribe
socketClient?.subscribeWithHeader(destination: destination, withHeader: header)
// unsubscribe
socketClient?.unsubscribe(destination: subsId)
You can use this feature if you need to auto reconnect with a spesific time or it will just try to reconnect every second.
// Reconnect after 4 sec
socketClient.reconnect(request: NSURLRequest(url: url as URL) , delegate: self as StompClientLibDelegate, time: 4.0)
// Auto Disconnect after 3 sec
socketClient.autoDisconnect(time: 3)
1.4.1 (2020-02-10)
Delegate protocol StompClientLibDelegate
now uses client: StompClientLib
instead of client: StompClientLib!
in its signatures.
1.4.0 (2020-02-06)
First version after I (michaelpeternell) forked the library.
- Improve documentation
- Better carthage support
- Better logging
See versions 1.3.6 and below at https://github.com/WrathChaos/StompClientLib