GithubHelp home page GithubHelp logo

isabella232 / nginx-nats Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nats-io/nginx-nats

0.0 0.0 0.0 85 KB

NGINX client module for NATS, the cloud native messaging system.

Home Page: https://nats.io

License: Apache License 2.0

C 100.00%

nginx-nats's Introduction

nginx-nats

License Apache 2.0

Nginx module that implements the NATS client.

Configuration:

NATS configuration is a section specified at the main level (i.e. not inside the http section).

    nats {
        server host1:port1;
        ...
        server hostN:portN;

        reconnect 2s;
        ping      30s;

        user      <username>;
        password  <password>;
    }

    http {
        ....
    }
  • One or more NATS servers can be specified. Nginx tries them in the listed order and connects to first available ("first working", not "round-robin" or other load-balancing).
  • If a connection cannot be created or NATS disconnects then Nginx tries all listed servers then waits for specified reconnect interval before it tries to connect again. Default reconnect interval is 1 second.
  • ping specified the interval at which Nginx sends PING messages to NATS server. Default is 30 seconds.
  • user and password are required if the NATS server has been configured to require authentication. Currently it applies to all servers; per-server user/password authentication is a possible future feature.

Build:

This module only maintains connections to NATS but does not do anything with those connections, so usually this module should be built together with some other module using the API which we export.

Like any nginx module, building nginx-nats is handled by configuring an nginx build to reference this module, then building nginx.

This module depends upon:

  • OpenSSL (currently just for randomness, but in the future for secured connections)
nginx-src-dir$ ./configure [...] \
  --add-module=/path/to/github.com/apcera/nginx-nats/src \
  --add-module=/path/to/module/which/uses/nats \
  --with-cc-opt=-I${OPENSSLDIR:?}/include \
  "--with-ld-opt=-L${OPENSSLDIR:?}/lib -lssl -lcrypto [...]"
nginx-src-dir$ make

API

There are two available header files

  • ngx_nats.h -- core of the features
  • ngx_nats_json.h -- interacting with NATS via JSON objects

All JSON methods, types and macro constants begin ngx_nats_json (or the upper-case version of that).

ngx_nats.h

The core type is ngx_nats_client_t, which is a struct type, thus variables will be of type ngx_nats_client_t *. The contents are outlined below, after the callback functions.

The outline of the API is below, see the header for parameters and types.

Basic usage:

  • ngx_nats_add_client() -- register a client which is using nginx-nats
  • ngx_nats_publish() -- publish a message, serialized; optionally in reply to another message
  • ngx_nats_subscribe() -- subscribe to receive messages
  • ngx_nats_unsubscribe() -- end a subscription
  • ngx_nats_create_inbox() -- create an end-point to receive a reply to a message

There are three callback functions which are needed:

  • ngx_nats_connected_pt -- as (ngx_nats_client_t*).connected
  • ngx_nats_disconnected_pt -- as (ngx_nats_client_t*).disconnected
  • ngx_nats_handle_msg_pt -- passed as parameter to ngx_nats_subscribe()

A ngx_nats_client_t *client contains three members, which should be set by the caller:

  • .connected -- callback function
  • .disconnected -- callback function
  • .data -- a void * callback data blob for use by the caller

Utilities:

  • ngx_nats_init_random() -- initialise randomness in the nginx-nats module
  • ngx_nats_next_random() โ†’ uint32_t: get 32-bits of randomness
  • ngx_nats_get_local_ip() โ†’ ngx_addr_t * (or NULL) -- our local IP

ngx_nats_json.h

JSON structure types: ngx_nats_json_object_t, ngx_nats_json_array_t, ngx_nats_json_value_t, ngx_nats_json_field_t.

  • ngx_nats_json_parse(): parse a serialized message into JSON structure types, using the provided allocation pool
  • ngx_nats_json_type_name(): convert an NGX_NATS_JSON_* type constant into a string name

License

Unless otherwise noted, the NATS source files are distributed under the Apache Version 2.0 license found in the LICENSE file.

nginx-nats's People

Contributors

colinsullivan1 avatar derekcollison avatar gcolliso avatar khazanovsky avatar kozlovic avatar levb avatar sgoldsm avatar tcahill avatar tylertreat avatar wallyqs avatar zquestz 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.