Coder Social home page Coder Social logo

swiftstomp's Introduction

SwiftStomp

An elegent Stomp client for swift, Base on Starscream websocket library.

Version License Platform

SwiftStomp

Fetures

  • Easy to setup, Very light-weight
  • Support all STOMP V1.2 frames. CONNECT, SUBSCRIBE, RECEIPT and ....
  • Auto object serialize using native JSON Encoder.
  • Send and receive Data and Text
  • Auto reconnect
  • Logging

Usage

Setup

Quick initialize with minimum requirements:

let url = URL(string: "ws://192.168.88.252:8081/socket")!
        
self.swiftStomp = SwiftStomp(host: url) //< Create instance
self.swiftStomp.delegate = self //< Set delegate
self.swiftStomp.autoReconnect = true //< Auto reconnect on error or cancel

self.swiftStomp.connect() //< Connect

Delegate

Implement all delegate methods to handle all STOMP events!

func onConnect(swiftStomp : SwiftStomp, connectType : StompConnectType)
    
func onDisconnect(swiftStomp : SwiftStomp, disconnectType : StompDisconnectType)

func onMessageReceived(swiftStomp: SwiftStomp, message: Any?, messageId: String, destination: String, headers : [String : String])

func onReceipt(swiftStomp : SwiftStomp, receiptId : String)

func onError(swiftStomp : SwiftStomp, briefDescription : String, fullDescription : String?, receiptId : String?, type : StompErrorType)

Connect

Full Connect signature:

self.swiftStomp.connect(timeout: 5.0, acceptVersion: "1.1,1.2")

If you want to reconnect after any un-expected disconnections, enable autoReconnect property.

self.swiftStomp.autoReconnect = true

Notice: If you disconnect manually using disconnect() function, and autoReconnect is enable, socket will try to reconnect after disconnection. If this is not thing you want, please disable autoReconnect before call the disconnect().

Subscription

Full Subsribe signature. Please notice to subscribe only when you ensure connected to the STOMP. I suggest do it in the onConnect delegate with connectType == .toStomp

swiftStomp.subscribe(to: "/topic/greeting", mode: .clientIndividual)

Send Message

You have full controll for sending messages. Full signature is as follows:

swiftStomp.send(body: "This is message's text body", to: "/app/greeting", receiptId: "msg-\(Int.random(in: 0..<1000))", headers: [:])

Connection Status check

You can check the status of the SwiftStomp by using connectionStatus property:

switch self.swiftStomp.connectionStatus {
case .connecting:
    print("Connecting to the server...")
case .socketConnected:
    print("Scoket is connected but STOMP as sub-protocol is not connected yet.")
case .fullyConnected:
    print("Both socket and STOMP is connected. Ready for messaging...")
case .socketDisconnected:
    print("Socket is disconnected")
}

Test Environment

This example was test with a Spring Boot websocket server with RabbitMQ as an external message broker.

Example

Please refer to the Example for more functionalities

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

Requirements

  • iOS 10 or above

Installation

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

pod 'SwiftStomp'

Author

Ahmad Daneshvar, [email protected]

License

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

swiftstomp's People

Contributors

romixery avatar bently93 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.