Convert Figma logo to code with AI

moleculerjs logomoleculer

:rocket: Progressive microservices framework for Node.js

6,146
583
6,146
69

Top Related Projects

66,731

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript πŸš€

15,054

The API and real-time application framework

22,821

Realtime MVC Framework for Node.js

16,828

AdonisJS is a TypeScript-first web framework for building web apps and API servers. It comes with support for testing, modern tooling, an ecosystem of official packages, and more.

63,346

πŸš€ Strapi is the leading open-source headless CMS. It’s 100% JavaScript/TypeScript, fully customizable, and developer-first.

31,844

Fast and low overhead web framework, for Node.js

Quick Overview

Moleculer is a fast, modern, and powerful microservices framework for Node.js. It helps developers build efficient, reliable, and scalable services using a modular approach. Moleculer provides features like service discovery, load balancing, and fault tolerance out of the box.

Pros

  • Fast and lightweight, with excellent performance benchmarks
  • Built-in service registry and dynamic service discovery
  • Supports multiple transport layers (TCP, NATS, Redis, MQTT, etc.)
  • Extensive middleware and plugin system for easy customization

Cons

  • Steeper learning curve compared to simpler frameworks
  • Limited ecosystem compared to more established frameworks
  • Documentation can be sparse for advanced use cases
  • Primarily focused on Node.js, limiting language choices

Code Examples

Creating a simple service:

const { ServiceBroker } = require("moleculer");

const broker = new ServiceBroker();

broker.createService({
    name: "math",
    actions: {
        add(ctx) {
            return Number(ctx.params.a) + Number(ctx.params.b);
        }
    }
});

broker.start()
    .then(() => broker.call("math.add", { a: 5, b: 3 }))
    .then(res => console.log("5 + 3 =", res))
    .catch(err => console.error(err));

Using built-in caching:

module.exports = {
    name: "posts",
    actions: {
        list: {
            cache: {
                keys: ["limit", "offset"],
                ttl: 30
            },
            handler(ctx) {
                // Fetch posts from database
            }
        }
    }
};

Implementing a middleware:

const broker = new ServiceBroker({
    middlewares: [
        {
            localAction(next, action) {
                return function(ctx) {
                    console.log(`Action '${action.name}' called`);
                    return next(ctx);
                };
            }
        }
    ]
});

Getting Started

  1. Install Moleculer:

    npm install moleculer
    
  2. Create a new service file (e.g., math.service.js):

    module.exports = {
        name: "math",
        actions: {
            add(ctx) {
                return Number(ctx.params.a) + Number(ctx.params.b);
            }
        }
    };
    
  3. Create a broker and start the service:

    const { ServiceBroker } = require("moleculer");
    const broker = new ServiceBroker();
    
    broker.loadService("./math.service.js");
    broker.start()
        .then(() => broker.call("math.add", { a: 5, b: 3 }))
        .then(res => console.log("5 + 3 =", res))
        .catch(err => console.error(err));
    
  4. Run the application:

    node app.js
    

Competitor Comparisons

66,731

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript πŸš€

Pros of Nest

  • More comprehensive ecosystem with built-in support for various features like GraphQL, WebSockets, and microservices
  • Strong TypeScript integration and decorators for a more structured, object-oriented approach
  • Larger community and more extensive documentation

Cons of Nest

  • Steeper learning curve due to its opinionated structure and extensive features
  • Heavier framework with potentially slower startup times compared to Moleculer
  • More complex setup for simple applications

Code Comparison

Nest:

@Controller('cats')
export class CatsController {
  @Get()
  findAll(): string {
    return 'This action returns all cats';
  }
}

Moleculer:

module.exports = {
  name: "cats",
  actions: {
    list: {
      handler() {
        return "This action returns all cats";
      }
    }
  }
};

Summary

Nest offers a more comprehensive ecosystem with stronger TypeScript integration, making it suitable for large, complex applications. However, it has a steeper learning curve and may be overkill for simpler projects. Moleculer, on the other hand, provides a lighter, more flexible approach that can be easier to set up and use for microservices, but may lack some of the built-in features and structure that Nest provides out of the box.

15,054

The API and real-time application framework

Pros of Feathers

  • More mature and established ecosystem with a larger community
  • Built-in support for real-time events and WebSocket connections
  • Extensive plugin system for easy integration of additional functionality

Cons of Feathers

  • Less focus on microservices architecture compared to Moleculer
  • Can be more complex to set up and configure for larger applications
  • Limited built-in support for service discovery and load balancing

Code Comparison

Feathers service example:

module.exports = {
  create(data, params) {
    return Promise.resolve(data);
  },
  find(params) {
    return Promise.resolve([]);
  }
};

Moleculer service example:

module.exports = {
  name: "users",
  actions: {
    create(ctx) {
      return ctx.params;
    },
    list(ctx) {
      return [];
    }
  }
};

Both frameworks offer simple and intuitive ways to define services, but Moleculer's approach is more explicitly focused on actions and service names. Feathers uses a more RESTful naming convention for its service methods.

Feathers is well-suited for real-time applications and projects that require extensive customization through plugins. Moleculer, on the other hand, excels in microservices architectures and distributed systems, offering built-in features for service discovery and load balancing.

22,821

Realtime MVC Framework for Node.js

Pros of Sails

  • More mature and established framework with a larger community
  • Built-in ORM (Waterline) for database interactions
  • Comprehensive documentation and extensive tutorials

Cons of Sails

  • Heavier and more opinionated, which can be limiting for some projects
  • Less focus on microservices architecture
  • Slower development and release cycle compared to Moleculer

Code Comparison

Sails (Creating a route):

// config/routes.js
module.exports.routes = {
  'GET /hello': 'HelloController.index'
};

Moleculer (Creating a service):

// services/hello.service.js
module.exports = {
  name: "hello",
  actions: {
    index(ctx) {
      return "Hello, World!";
    }
  }
};

Sails follows a more traditional MVC structure, while Moleculer focuses on service-based architecture. Sails provides a more comprehensive out-of-the-box solution, including ORM and asset pipeline, whereas Moleculer offers a lightweight and flexible approach to building microservices.

Sails is better suited for developers who prefer a full-featured framework with built-in conventions, while Moleculer is ideal for those who want a modular and scalable microservices architecture with less overhead.

16,828

AdonisJS is a TypeScript-first web framework for building web apps and API servers. It comes with support for testing, modern tooling, an ecosystem of official packages, and more.

Pros of AdonisJS

  • Full-featured MVC framework with built-in ORM, authentication, and more
  • Strong TypeScript support and extensive documentation
  • Follows convention over configuration, leading to faster development

Cons of AdonisJS

  • Steeper learning curve due to its comprehensive nature
  • Less flexible for microservices architecture compared to Moleculer
  • Heavier footprint and potentially slower startup times

Code Comparison

AdonisJS (Route definition):

Route.get('/', async ({ view }) => {
  return view.render('welcome')
})

Moleculer (Service definition):

module.exports = {
  name: "greeter",
  actions: {
    hello() {
      return "Hello Moleculer"
    }
  }
}

AdonisJS focuses on a traditional MVC structure with a powerful routing system, while Moleculer emphasizes a microservices approach with service-based architecture. AdonisJS provides a more opinionated and integrated development experience, whereas Moleculer offers greater flexibility for distributed systems.

AdonisJS is better suited for monolithic applications or when you need a full-stack framework with built-in features. Moleculer shines in microservices environments and when you require a lightweight, high-performance solution for distributed systems.

Choose AdonisJS for rapid development of feature-rich web applications, and opt for Moleculer when building scalable, distributed microservices architectures.

63,346

πŸš€ Strapi is the leading open-source headless CMS. It’s 100% JavaScript/TypeScript, fully customizable, and developer-first.

Pros of Strapi

  • User-friendly admin panel for content management
  • Built-in authentication and user management
  • Extensive plugin ecosystem for easy customization

Cons of Strapi

  • Steeper learning curve for developers new to headless CMS
  • Less flexibility for complex microservices architectures
  • Potentially higher resource usage for large-scale applications

Code Comparison

Strapi (Content Type definition):

module.exports = {
  attributes: {
    title: {
      type: 'string',
      required: true,
    },
    content: {
      type: 'richtext',
    },
  },
};

Moleculer (Service definition):

module.exports = {
  name: "posts",
  actions: {
    create(ctx) {
      // Create a new post
    },
    list(ctx) {
      // List all posts
    }
  }
};

Strapi is a headless CMS that excels in content management with a user-friendly interface, while Moleculer is a microservices framework focused on building scalable and distributed systems. Strapi provides a more opinionated structure for content-driven applications, whereas Moleculer offers greater flexibility for custom microservices architectures. The code comparison illustrates Strapi's content type definition approach versus Moleculer's service-oriented structure.

31,844

Fast and low overhead web framework, for Node.js

Pros of Fastify

  • Extremely fast performance, often outperforming other popular Node.js frameworks
  • Built-in support for JSON Schema validation
  • Highly extensible plugin system

Cons of Fastify

  • Steeper learning curve compared to Moleculer
  • Less focus on microservices architecture out of the box

Code Comparison

Fastify:

const fastify = require('fastify')()

fastify.get('/', async (request, reply) => {
  return { hello: 'world' }
})

fastify.listen(3000)

Moleculer:

const { ServiceBroker } = require("moleculer");
const broker = new ServiceBroker();

broker.createService({
    name: "greeter",
    actions: {
        hello() {
            return "Hello Moleculer"
        }
    }
});

broker.start()

Fastify is a web framework focused on providing high performance and low overhead for HTTP server applications. It excels in building fast and efficient RESTful APIs.

Moleculer, on the other hand, is a microservices framework that provides a complete solution for building distributed systems. It includes features like service discovery, load balancing, and built-in caching.

While Fastify is more suited for single-server applications with a focus on HTTP performance, Moleculer shines in distributed architectures and complex microservices ecosystems.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Moleculer logo

CI test Coverage Status Maintainability Codacy Badge Known Vulnerabilities Discord chat

Downloads FOSSA Status Patreon

Moleculer NPM version Twitter URL

Moleculer is a fast, modern and powerful microservices framework for Node.js. It helps you to build efficient, reliable & scalable services. Moleculer provides many features for building and managing your microservices.

Website: https://moleculer.services

Documentation: https://moleculer.services/docs

Top sponsors

What's included

  • Promise-based solution (async/await compatible)
  • request-reply concept
  • support event driven architecture with balancing
  • built-in service registry & dynamic service discovery
  • load balanced requests & events (round-robin, random, cpu-usage, latency, sharding)
  • many fault tolerance features (Circuit Breaker, Bulkhead, Retry, Timeout, Fallback)
  • plugin/middleware system
  • support versioned services
  • support Streams
  • service mixins
  • built-in caching solution (Memory, MemoryLRU, Redis)
  • pluggable loggers (Console, File, Pino, Bunyan, Winston, Debug, Datadog, Log4js)
  • pluggable transporters (TCP, NATS, MQTT, Redis, NATS Streaming, Kafka, AMQP 0.9, AMQP 1.0)
  • pluggable serializers (JSON, Avro, MsgPack, Protocol Buffer, Thrift, CBOR, Notepack)
  • pluggable parameter validator
  • multiple services on a node/server
  • master-less architecture, all nodes are equal
  • built-in parameter validation with fastest-validator
  • built-in metrics feature with reporters (Console, CSV, Datadog, Event, Prometheus, StatsD)
  • built-in tracing feature with exporters (Console, Datadog, Event, Jaeger, Zipkin, NewRelic)
  • official API gateway, Database access and many other modules...

Installation

$ npm i moleculer

or

$ yarn add moleculer

Create your first microservice

This example shows you how to create a small service with an add action which can add two numbers and how to call it.

const { ServiceBroker } = require("moleculer");

// Create a broker
const broker = new ServiceBroker();

// Create a service
broker.createService({
    name: "math",
    actions: {
        add(ctx) {
            return Number(ctx.params.a) + Number(ctx.params.b);
        }
    }
});

// Start broker
broker.start()
    // Call service
    .then(() => broker.call("math.add", { a: 5, b: 3 }))
    .then(res => console.log("5 + 3 =", res))
    .catch(err => console.error(`Error occurred! ${err.message}`));

Try it in your browser

Create a Moleculer project

Use the Moleculer CLI tool to create a new Moleculer based microservices project.

  1. Create a new project (named moleculer-demo)

    $ npx moleculer-cli -c moleculer init project moleculer-demo
    
  2. Open the project folder

    $ cd moleculer-demo
    
  3. Start the project

    $ npm run dev
    
  4. Open the http://localhost:3000/ link in your browser. It shows a welcome page that contains more information about your project & you can test the generated services.

:tada: Congratulations! Your first Moleculer-based microservices project is created. Read our documentation to learn more about Moleculer.

Welcome page

Official modules

We have many official modules for Moleculer. Check our list!

Supporting

Moleculer is an open source project. It is free to use for your personal or commercial projects. However, developing it takes up all our free time to make it better and better on a daily basis. If you like Moleculer framework, please support it.

Thank you very much!

For enterprise

Available as part of the Tidelift Subscription.

The maintainers of moleculer and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

Documentation

You can find here the documentation.

Changelog

See CHANGELOG.md.

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

Contributions

We welcome you to join in the development of Moleculer. Please read our contribution guide.

Project activity

Alt

License

Moleculer is available under the MIT license.

3rd party licenses

Contact

Copyright (c) 2016-2023 MoleculerJS

@moleculerjs @MoleculerJS

NPM DownloadsLast 30 Days