Git Product home page Git Product logo

nats-swift's Introduction

NATS Client implemented in swift

NATS is an open-source, cloud-native messaging system. Companies like Apcera, Baidu, Siemens, VMware, HTC, and Ericsson rely on NATS for its highly performant and resilient messaging capabilities.

Its Nats Client/Streaming implemented in swift

Build for Vapor 3 depends on SwiftProto and Swift NIO

If you have ideas to improve any part of the code please contact me

Configuration file:

    services.register(NatsRouter.self) { _ -> (RouterController) in
        return RouterController()
    }
    
    services.register { container -> (NATS) in
        let servers = [natsServer(hostname: "0.0.0.0")]
        
        let natsConfig = NatsClientConfig(servers: servers, connectionType: .server(.multiple(1)), disBehavior: .reconnect, clusterName: "_STAN.discover.test-cluster", streaming: true)
        return try NATS(container: container, config: natsConfig)
    }

Note:

Connection type

  1. Server with multiple eventloops (Each eventloop creates separate connection to Nats server)
  2. Client(Creates connection if it does not have one for current eventloop)

RouterController file:

class RouterController: Service, NatsRouter {
    func onOpen(handler: NatsHandler, ctx: ChannelHandlerContext) {
        print("OPEN")
    }
    
    func onStreamingOpen(handler: NatsHandler, ctx: ChannelHandlerContext) {
        print("STREAM OPEN")
    }
    
    func onClose(handler: NatsHandler, ctx: ChannelHandlerContext) {
        print("CLOSED")
    }
    
    func onError(handler: NatsHandler, ctx: ChannelHandlerContext, error: Error) {
        print("ERROR -> \(error.localizedDescription)")
    }
}

Subscription:

    handler.subscribe("Subject") { (msg) in
            
    }

nats-swift's People

Contributors

hjuraev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.