Skip to content

scalecube/scalecube-js

Repository files navigation

Scalecube-js

Scalecube is a toolkit for creating microservices/micro-frontends based systems.
what is scalecube-js | play with codesandbox | full documentation

Join the chat at https://gitter.im/scalecube-js/Lobby

Project Status

scalecube-js curretly not maintained.
If you intested become a maintainer you can reach me at https://www.linkedin.com/in/idanilt/.

quick start

If you are new to scalecube, it's recommended to read this introduction to scalecube.

Scalecube provide browser and NODE templates, configured and ready for use

yarn add @scalecube/browser or npm i @scalecube/browser

import { createMicroservice, ASYNC_MODEL_TYPES } from '@scalecube/browser';

yarn add @scalecube/node or npm i @scalecube/node

import { createMicroservice, ASYNC_MODEL_TYPES } from '@scalecube/node';

Advanced

You can create your own customized setup, for more details: go to Microservice

Usage (Browser and node)

create a seed

// node - supported WS, WSS and TCP
export const MySeedAddress: 'ws://localhost:8000';
// Browser - under browser post message will be used as transport
export const MySeedAddress: 'seed';

// Create a service
createMicroservice({
   address : MySeedAddress
});

Create a service

// Create service definition
export const greetingServiceDefinition = {
  serviceName: 'GreetingService',
  methods: { 
    hello: {
      asyncModel: ASYNC_MODEL_TYPES.REQUEST_RESPONSE,
    }
  },
};
// Create a service
createMicroservice({
  services : [{
    definition: greetingServiceDefinition,
    reference: {
      hello : async (name) => `Hello ${name}`
    }, 
   }],
   seedAddress : MySeedAddress
});

Use a service

const microservice = createMicroservice({seedAddress : MySeedAddress})

// With proxy
const greetingService = microservice.createProxy({
    serviceDefinition: greetingServiceDefinition
});

greetingService.hello('ME').then(console.log) // Hello ME

*We let Scalecube choose our addresses for us, we know only the seed address.
After we connected to the seed we will see the whole cluster. In the browser we don't need to import modules, we can create multiple bundles, scalecube will discover the available services

*NOTICE For Node you have to set addresses, there isn't any default at the moment

Worker, Iframes and seperate bundles

You can use scalecube with multiple bundles even inside iframes and *Web Workers, scalecube will be able to find and invoke the services by the address and seedAddress (via Post Messages).

*NOTICE In order to use Web Workers you need to load scalecube before loading the Web Worker (scalecube extends Worker class)

Good

import from "@scalecube/browser";

new Worker(`assets/worker.js`, {
  type: "module"
});

Bad

new Worker(`assets/worker.js`, {
  type: "module"
});

Dependency Injection

You can pass a function instead of object as the reference, this function get createProxy and createServiceCall, which can help you invoke any service in the cluster. The function need to return object with all the service methods ([methodName]: function()).
In the example bellow, we are creating a new instance of a class, but you can do in anyway you want.

createMicroservice({
  seedAddress : MySeedAddress,
  services: [
    {
      definition: serviceB,
      reference: ({ createProxy, createServiceCall }) => {
        const greetingService = createProxy({serviceDefinition: greetingServiceDefinition });

        return new ServiceB(greetingService);
      }
    }    
  ]
})

For more examples go to examples or full documentation

Scalecube tools

Router,
Discovery,
Transport-browser,
Transport-nodejs,
Gateway,
Cluster-browser,
Cluster-nodejs.

Version

MAJOR version when you make incompatible API changes,

MINOR version when you add functionality in a backwards-compatible manner, and

PATCH version when you make backwards-compatible bug fixes.