mmpreactivestompclient is a comprehensive library that provides a reactive client for STOMP (Simple Text Oriented Messaging Protocol) communication in iOS using Swift. This library facilitates the exchange of messages between web servers and iOS applications over WebSocket-based protocols.
Features
- Supports STOMP protocol version 1.0, 1.1, and 1.2.
- Reactive programming model using RxSwift.
- WebSocket-based communication.
- Implementation of STOMP clients and producers.
- Supports both sending and receiving messages.
- Error handling and connection management.
Requirements
- iOS 10.0 or later.
- Swift 5.0 or later.
Installation
To integrate mmpreactivestompclient into your project, follow these steps:
- Install Carthage if not already installed.
- Create a
Cartfile
at the root of your project directory. - Add the following line to your
Cartfile
:
github "matteompi/mmpreactivestompclient-ios" ~> 1.0.0
- Open a terminal and navigate to your project directory.
- Run the following command:
carthage update --platform iOS
- Drag and drop the built
StompClient.framework
into your Xcode project. - In Xcode, go to your target’s “General” settings tab.
- In the “Frameworks, Libraries, and Embedded Content” section, add the
StompClient.framework
. - Import the framework in your code:
import StompClient
Getting Started
In order to establish a connection and exchange messages using STOMP, follow these steps:
- Create an instance of
StompClient
. - Set the connection delegate and message delegate.
- Connect to the server using your desired WebSocket transport layer implementation.
- Subscribe to STOMP destinations to receive messages.
- Send messages to the server.
- Handle received messages and errors using protocol callbacks.
- Disconnect from the server when needed.
Example Code
Here’s an example to get you started:
import UIKit
import StompClient
import RxSwift
class ViewController: UIViewController {
private let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
let stompClient = StompClient(url: URL(string: "wss://your.websocket.url")!)
stompClient.connectionDelegate = self
stompClient.messageDelegate = self
stompClient.connect(transport: .yourTransportImplementation)
.subscribe(onConnect: { [weak self] in
// Handle connection success
self?.stompClient.subscribe(destination: "/topic/your_topic")
}, onError: { error in
// Handle connection error
}).disposed(by: disposeBag)
}
}
extension ViewController: StompConnectionDelegate {
func stompClientDidDisconnect(client: StompClient) {
// Handle disconnection
}
}
extension ViewController: StompMessageDelegate {
func stompClient(client: StompClient, didReceiveMessage message: StompMessage) {
// Handle received message
}
func stompClientDidReceivePong(client: StompClient) {
// Handle received pong message
}
}
Conclusion
mmpreactivestompclient provides a convenient way to establish STOMP communication in your iOS applications. With its support for different STOMP protocol versions and error handling, you can easily integrate real-time messaging into your projects.