WrathChaos / StompClientLib

Simple STOMP Client library, Swift 3 and 4, 4.2, 5 compatible
https://www.freakycoder.com
MIT License
154 stars 81 forks source link
apple client ios ios-app ios-development mobile socketrocket stomp stompclient stompclientlib swift swift5 websocket xcode

StompClientLib

License platform Cocoapods

Swift 5.0 Swift 4.2 Swift 3.0 Pod Version Issues

Swift 5+ StompClient Library

Introduction

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+, 4+ and 3+ projects.

Supported Stomp Versions

Stomp version

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

StompClientLib is available through CocoaPods. To install it, simply add the following line to your Podfile:

Cocoapods

pod "StompClientLib"

Carthage

github "WrathChaos/StompClientLib"

Usage

import StompClientLib

Once imported, you can open a connection to your WebSocket server.

var socketClient = StompClientLib()
let url = NSURL(string: "your-socket-url-is-here")!
socketClient.openSocketWithURLRequest(request: NSURLRequest(url: url as URL) , delegate: self)

After you are connected, there are some delegate methods that you need to implement.

StompClientLibDelegate

stompClientDidConnect

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
}

stompClientDidDisconnect

func stompClientDidDisconnect(client: StompClientLib!) {
print("Socket is Disconnected")
}

didReceiveMessageWithJSONBody ( Message Received via STOMP )

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")")
}

didReceiveMessageWithJSONBody ( Message Received via STOMP as String )

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("DESTINATION : \(destination)")
  print("String JSON BODY : \(String(describing: jsonBody))")
}

serverDidSendReceipt

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)")
}

serverDidSendError

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))")
}

serverDidSendPing

If you need to control your server's ping, here is your part

func serverDidSendPing() {
  print("Server ping")
}

How to subscribe and unsubscribe

There are functions for subscribing and unsubscribing. Note : You should handle your subscribe and unsubscribe methods ! Suggestion : Subscribe to your topic in "stompClientDidConnect" function and unsubcribe to your topic in stompClientWillDisconnect method.

Subscribe

socketClient.subscribe(destination: topic)
// Note : topic needs to be a String object

Unsubscribe

socketClient.unsubscribe(destination: topic)

Important : You have to send your destination for both subscribe or unsubscribe!

Unsubsribe with header

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)

Auto Reconnect with a given time

You can use this feature if you need to auto reconnect with a specific 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 with a given time

// Auto Disconnect after 3 sec
socketClient.autoDisconnect(time: 3)

Login Passcode Implementation

This is just an example. You need to convert to your implementation. #42

let connectFrame = "CONNECT\n login:admin\n passcode:password\n\n\n\0"
socket.write(string: connectFrame)

Future Enhancements

Changelog

Full Changelog

Implemented enhancements:

Closed issues:

1.3.8 (2020-03-08)

Full Changelog

Implemented enhancements:

Fixed bugs:

Closed issues:

Merged pull requests:

1.3.7 (2019-08-26)

Full Changelog

Closed issues:

1.3.6 (2019-08-06)

Full Changelog

Implemented enhancements:

Fixed bugs:

Closed issues:

Merged pull requests:

1.3.5 (2019-07-25)

Full Changelog

Fixed bugs:

Closed issues:

Merged pull requests:

1.3.4 (2019-07-19)

Full Changelog

1.3.3 (2019-07-18)

Full Changelog

Closed issues:

Merged pull requests:

1.3.2 (2019-07-10)

Full Changelog

Implemented enhancements:

1.3.1 (2019-06-14)

Full Changelog

Closed issues:

1.3.0 (2019-04-30)

Full Changelog

Implemented enhancements:

Closed issues:

1.2.7 (2018-10-23)

Full Changelog

1.2.6 (2018-10-23)

Full Changelog

Fixed bugs:

Closed issues:

1.2.5 (2018-10-22)

Full Changelog

Closed issues:

1.2.4 (2018-10-17)

Full Changelog

Closed issues:

1.2.3 (2018-10-17)

Full Changelog

Implemented enhancements:

Closed issues:

1.2.2 (2017-11-03)

Full Changelog

1.2.1 (2017-10-31)

Full Changelog

1.2.0 (2017-10-29)

Full Changelog

Closed issues:

1.1.7 (2017-10-02)

Full Changelog

1.1.6 (2017-08-08)

Full Changelog

0.1.5 (2017-07-10)

Full Changelog

0.1.4 (2017-07-10)

Full Changelog

0.1.3 (2017-07-10)

Full Changelog

0.1.2 (2017-07-08)

Full Changelog

0.1.1 (2017-07-08)

Full Changelog

0.1.0 (2017-07-08)

Full Changelog

* _This Changelog was automatically generated by github_changelog_generator_

Author

FreakyCoder, kurayogun@gmail.com

License

StompClientLib is available under the MIT license. See the LICENSE file for more info.