Socket
Book a DemoInstallSign in
Socket

ecc-messagebus

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ecc-messagebus

Eccenca Message Bus for inter-component and in-app communications.

latest
Source
npmnpm
Version
3.6.0
Version published
Maintainers
1
Created
Source

Eccenca Message Bus for inter-component and in-app communications

ecc-messagebus exports a normal rxmq.js instance but with a set of additional functions. One of those functions is the createChannels helper. For convenience reasons it also exports Rx so that we can use a fixed Rx version in all components.

Using Rx

import {Rx} from 'ecc-messagebus';

var source = Rx.Observable.just(42);

var subscription = source.subscribe(
  function (x) {
    console.log(`Next: ${x}');
  },
  function (err) {
    console.log(`Error: ${err}`);
  },
  function () {
    console.log('Completed');
  });

// => Next: 42
// => Completed

Using request-response

Request-response pattern can be used like so:

import rxmq from 'ecc-messagebus';
// ...
// get channel
const channel = rxmq.channel('yourChannel');
// subscribe to topic
channel.subject('someTopic').subscribe(({data, replySubject}) => {
    // ...
    // use envelop.reply to send response
    replySubject.onNext({some: 'response'});
    replySubject.onCompleted();
});
// ...
// initiate request and handle response as a promise
channel.request({
    topic: 'someTopic',
    data: {test: 'test'},
    timeout: 2000
})
.subscribe((data) => {
    // work with data here
    // ...
},
(err) => {
    // catch and handle error here
    // ...
});

Creating Channel Definitions

The createChannels functions allows to define channels and subjects. Basically it allows to create a channelDefinition with private and public subjects. The Channel of Private Subjects can be changed. This may sound counter-intuitive, but is actually a nice wrapper around changing channels. This helper is predominantly used within the ecc-mixins to create an event-bus per component instance, so that there are no side effects (two components reacting on the same click handler)


// in a channelDefinition.js

import {createChannels, SubjectType} from 'ecc-messagebus'

const {privateChannel, publicChannel} = createChannels({
    name: 'component',
    subjects: {
        secretMessage: SubjectType.private,
        update: SubjectType.public,
    }
});

export {privateChannel, publicChannel};

// in Component A

import {publicChannel, privateChannel} from './channelDefinition.js';

// Handle public messages
publicChannel.update.getSubject().subscribe(() => {
    console.log('There was an update');
});

// Handle Secret Message from Component B
publicChannel.secretMessage.getSubject('secret-key-b').subscribe(() => {
    console.log('Uuuuuh, a secret message from Component B');
});

// Handle Secret Message from Component C
publicChannel.secretMessage.getSubject('secret-key-c').subscribe(() => {
    console.log('Uuuuuh, a secret message from Component C');
});

// in Component B

import {privateChannel} from './channelDefinition.js';

// Send to Component A on Secret Channel
publicChannel.secretMessage.getSubject('secret-key-b').onNext('pssst, component A');

// in Component C

import {privateChannel} from './channelDefinition.js';

// Send to Component A on Secret Channel
publicChannel.secretMessage.getSubject('secret-key-c').onNext('pssst, component A');

FAQs

Package last updated on 24 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.