Seneca Microservices Framework

repository·master·Indexed 26 days ago

https://github.com/senecajs/seneca

A Node.js toolkit for building microservice architectures. Seneca allows developers to organize business logic into discrete, pattern-matched commands (plugins) that can be distributed across a network. It features a transport-independent request/response protocol supporting synchronous and asynchronous flows, with core API methods including .add() for defining patterns, .act() and .post() for executing commands, and .client() and .listen() for network configuration.

Tokens
2.7K
Snippets
10
Records
20
Agent score
87%

What's inside seneca

  1. Understand the Seneca message transport protocol

    master

    The Seneca protocol uses a request/response model that is transport-independent, assuming JSON documents can be delivered as discrete units. It supports synchronous requests, asynchronous actor/pub/sub flows, and chained synchronous flows.

    Key concepts:

    • Message: A single outbound request JSON document. A response is considered part of the message concept.
    • meta$: A reserved property for Seneca metadata. If missing, Seneca constructs it with default values to allow manual interactions (e.g., via curl).
    • Correlation ID (cid): Retained across service instances to trace the entire causal chain of a message flow.
    • Message ID (mid): A unique identifier for a specific message instance.
  2. Run Seneca microservices

    master

    To run a Seneca script normally:

    node microservice.js

    To run in test mode with human-readable, full debug logs:

    node microservice.js --seneca.test

    Note: Logs are output in JSON format by default, making them suitable for logging services.

    $ node microservice.js --seneca.test
  3. Run the Sales Tax example

    master

    To run the sales tax example, first ensure all dependencies are installed via npm install. Then, execute the scripts in the following specific sequence:

    1. node sales-tax.js
    2. node sales-tax-config.js

    Note: This specific example uses the original callback-based API. If you prefer using async/await, refer to the seneca-promisify package.

    npm install
    node sales-tax.js
    node sales-tax-config.js
  4. Trace message flows using the `trk` array

    master

    The trk (tracking) array provides a history of the message flow through various services.

    • Each entry in trk represents one outbound request and at most one response.
    • When an inbound message triggers new outbound messages, the originating Seneca instance is added to the tracking array.
    • The tms array within each tracking entry records UTC milliseconds for send/receive events, allowing for local processing time measurement.
  5. Initialize a Seneca instance

    master
    To use Seneca, call the default export (the init function) with an optional configuration object. You can pass a file path as a string to load options from a file, or pass an object. The returned instance is an EventEmitter and provides the core microservices API.
  6. Add patterns with `seneca.add()`

    master

    Use seneca.add(pattern, handler) to define a new command. The pattern is a JSON object that Seneca uses for pattern matching. The handler is a function that receives a message (msg) and a callback (done).

    Example of a simple pattern:

    var seneca = require('seneca')()
    
    seneca.add({cmd: 'salestax'}, function (msg, done) {
      var rate  = 0.23
      var total = msg.net * (1 + rate)
      done(null, {total: total})
    })
  7. Execute commands with `seneca.act()`

    master

    Use seneca.act(pattern, callback) to run a command. You can pass the pattern as a JSON object or as a string using an abbreviated JSON format (Jsonic).

    Using a JSON object:

    seneca.act({cmd: 'salestax', net: 100}, function (err, result) {
      console.log(result.total)
    })

    Using an abbreviated string:

    seneca.act('cmd:salestax,net:100', function (err, result) {
      console.log(result.total)
    })

    Combining pattern and data:

    seneca.act('cmd:salestax', {net: 100}, function (err, result) {
      console.log(result.total)
    })
  8. Listen for messages with `seneca.listen()`

    master

    The listen() method starts a service that listens for JSON messages. When messages arrive, they are submitted to the local Seneca instance and executed. By default, in process and http transports are included.

    Example of listening on a specific port with a pin:

    Seneca()
      .use(approver)
      .listen({type: 'http', port: '8260', pin: 'cmd:*'})
  9. Configure network clients with `seneca.client()`

    master

    Calling seneca.client() enables the Seneca instance to send actions it cannot match locally out over the network to configured clients. You can chain multiple .client() calls to specify different ports and pins.

    seneca.client({port: 8260, pin: 'cmd:run'})
      .client({port: 8270, pin: 'cmd:run'})
      .act('cmd:run', handler)
    seneca.client({port: 8260, pin: 'cmd:run'})
      .client({port: 8270, pin: 'cmd:run'})
      .use(local)
      .act('cmd:run', handler)