{"library":"moleculer","title":"Moleculer Microservices Framework","description":"Moleculer is a fast, modern, and powerful microservices framework for Node.js, designed to build efficient, reliable, and scalable distributed systems. It provides a comprehensive set of features including a promise-based request-reply mechanism, event-driven architecture, dynamic service discovery, load balancing, and fault tolerance capabilities like Circuit Breaker and Retry. The current stable version is 0.15.0, with frequent minor and patch releases, and major updates approximately annually, often introducing significant breaking changes. Key differentiators include its pluggable architecture for transporters (e.g., NATS, Redis, Kafka), serializers (e.g., MsgPack, CBOR), caching, loggers, and metrics/tracing reporters. It emphasizes a master-less architecture, making all nodes equal, and comes with built-in parameter validation and extensive TypeScript support.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install moleculer"],"cli":{"name":"moleculer","version":null}},"imports":["import { ServiceBroker } from 'moleculer';","import { Service } from 'moleculer';","import { ServiceSchema } from 'moleculer';","import { Transporters } from 'moleculer';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { ServiceBroker, ServiceSchema } from 'moleculer';\n\n// Create a ServiceBroker\nconst broker = new ServiceBroker({\n  nodeID: 'node-greeter-1',\n  logLevel: 'info',\n  transporter: 'NATS' // Or 'Redis', 'TCP', etc.\n});\n\n// Define a Greeter Service\nconst GreeterService: ServiceSchema = {\n  name: 'greeter',\n  settings: {\n    defaultName: 'World'\n  },\n  actions: {\n    hello: {\n      rest: 'GET /hello',\n      async handler(ctx) {\n        return `Hello ${ctx.params.name || this.settings.defaultName}`;\n      }\n    },\n    welcome: {\n      async handler(ctx) {\n        this.logger.info(`Welcoming ${ctx.params.name}...`);\n        return `Welcome, ${ctx.params.name}`;\n      }\n    }\n  },\n  events: {\n    'user.created': {\n      async handler(ctx) {\n        this.logger.info(`User created event received for ${ctx.params.username}`);\n      }\n    }\n  },\n  async started() {\n    this.logger.info(`Greeter service started on node ${broker.nodeID}`);\n  },\n  async stopped() {\n    this.logger.info(`Greeter service stopped on node ${broker.nodeID}`);\n  }\n};\n\n// Create a service from the schema\nbroker.createService(GreeterService);\n\n// Start the broker\nbroker.start()\n  .then(async () => {\n    // Call an action\n    const res = await broker.call('greeter.hello', { name: 'Moleculer' });\n    console.log(res);\n\n    // Emit an event\n    await broker.emit('user.created', { username: 'Alice' });\n\n    // Call an action from another node if available (assuming multiple nodes)\n    const welcomeMsg = await broker.call('greeter.welcome', { name: 'Bob' });\n    console.log(welcomeMsg);\n\n    // Stop the broker after a delay or on signal\n    setTimeout(() => broker.stop(), 5000);\n  })\n  .catch(err => {\n    broker.logger.error('Error starting broker:', err);\n  });\n","lang":"typescript","description":"This quickstart demonstrates how to create a basic Moleculer service, define actions and events, start the service broker, and then call an action and emit an event.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}