Giter Club home page Giter Club logo

meshage's Introduction

meshage

A simple service mesh. Messages sent within the service mesh can be consistently partitioned across members of the cluster.

build status npm version npm downloads

Install

npm install meshage --save

Usage

Initialize some nodes and define a handler for a subject & message:

const {mesh, nats} from 'meshage';
const n1 = mesh(nats('nats://localhost:4222'));

await n1.subject('something')
    .on(SomeMessage, (message: SomeMessage) => {
        return 'reply';
    })
    .awaitRegistration();

const reply = await mesh(nats('nats://localhost:4222'))
    .subject('something')
    .send(new SomeMessage(), {
        wait: true | false, // Whether to wait for a reply or just fire the message.
        timeout: 1000 // Time in milliseconds to wait for a reply.
    });

Nats Support

To enable consistent hashing / partitioned message support one node in the "cluster" should be configured with the monitorUrl for the nats servers where subscription information can be obtained from. This will cause the node to periodically broadcast subscription data used to consistently hash & partition requests:

const n1 = mesh(nats({
    server: 'nats://localhost:4222',
    monitorUrl: 'http://localhost:8222'
}));

HTTP Support

Messages may also optionally be sent & received with an HTTP listener by wrapping the supplied backend:

const {mesh, nats, http} from 'meshage';
const n1 = mesh(http(nats('nats://localhost:4222'), 8080));
n1.subject('something')
    .on(SomeMessage, (message: SomeMessage) => {
        return 'reply';
    });

HTTP API

Send

Sends a message to be handled by a registered handler for the specified subject/message.

Path : /api/:subject/:partitionKey?

Path params :

  • subject - The logical name for the message handler subject.
  • partitionKey - (Optional) Identifier used to consistently partition the request to known handlers.

Query params:

  • messageName - (Dependant) The name of message. If the messageName is not supplied via this param, then it must be provided in the message body as a top-level key called name - e.g. {"name": "SomeMessage", ...}
  • wait - (Optional) Whether to wait for a reply or just fire the message.
  • timeout - (Optional) Time in milliseconds to wait for a reply.

Example:

Request:

curl -sX POST http://localhost:8080/api/some-subject/$RANDOM?messageName=echo \
     -H 'Content-Type: application/json' \
     -d '{"hello":"world"}'

Response:

{
  "echoed": {
    "hello": "world"
  }
}

Broadcast

Sends a message to all registered handlers for the specified subject/name pair.

URL : /api/broadcast/:subject

Path params :

  • subject - The logical name for the message handler subject.

Query params:

  • messageName - (Dependant) The name of message. If the messageName is not supplied via this param, then it must be provided in the message body as a top-level key called name - e.g. {"name": "SomeMessage", ...}
  • wait - (Optional) Whether to wait for a reply or just fire the message.
  • timeout - (Optional) Time in milliseconds to wait for replies. Replies not received before the timeout of omitted.

Example :

Request:

curl -sX POST http://localhost:8080/api/broadcast/some-subject?messageName=echo \
     -H 'Content-Type: application/json' \
     -d '{"hello":"world"}'

Response:

[
 {
   "echoed": {
     "hello": "world"
   }
 }, 
 {
   "echoed": {
     "hello": "world"
   }
 }
]

License

MIT

meshage's People

Contributors

github1 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

meshage's Issues

tls

Default to secure connections between nodes

sidecar proxy

Provide ability to register message handlers which proxy to other services.

Options

  • target: url to proxy to
  • timeout: optional request timeout
  • interceptor: function to modify raw request (e.g. add headers, manipulate body)
meshage
    .init(new meshage.GrapevineCluster())
    .register('echo', meshage.proxy({
       target: 'http://localhost:7000',
       timeout: 30,
       interceptor: req => {
       }
     }))
    .start();

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.