1.0.2 • Published 5 years ago

coremessaging v1.0.2

Weekly downloads
3
License
ISC
Repository
-
Last release
5 years ago

CoreMessaging

A library for build REST API in Node.js using in some personal project.

Installation

IMPORTANT: Requires Node.js 6 or newer.

npm install coremessaging

Quick Example

const coremessaging = require('coremessaging');

ExchangeFactory.createInstance({
    type: 'topic', // 'topic' | 'fanout' | 'direct'
    mode: 'redis', // 'redis' | 'memory'
    engine: {
        host: '127.0.0.1',
        port: 6379,
        password: 'my@passw0rd',
        database: 15
    }
    , LIMIT_POOL_THREAD: 4
    , LIMIT_STREAM: 2500
    , retry: 3
});

ExchangeFactory.createStream({ topic: 'photo' }, (msg, ack) => {
    let msTime = Math.floor(Math.random() * (1000 - 250) + 250);
    // setTimeout(() => {
    //     console.log(`> ${msTime} ms. handler has called!! photo =>`, msg);
    //     ack();
    // }, msTime);

    new Promise((resolve, reject) => {
        setTimeout(() => {
            if (msg.id % 3 == 0) {
                reject(new Error('Test ERR!! id=' + msg.id));
            } else {
                resolve();
                console.log(`> ${msTime} ms. handler has called!! photo =>`, msg);
            }
        }, msTime);
    }).then(() => {
        ack();
    }).catch(err => {
        ack(err);
        console.log('handler.err =>', err.message);
    });
});

ExchangeFactory.createStream({ topic: 'photo_in_folder' /*, LIMIT_POOL_THREAD: 4*/ }, (msg, ack) => {
    let msTime = Math.floor(Math.random() * (500 - 50) + 50);
    setTimeout(() => {
        console.log(`>> ${msTime} ms. handler has called!! photo_in_folder =>`, msg);
        ack();
    }, msTime);
});

for (let i = 0; i < 10; i++) {
    // ExchangeFactory.send(/*msg :=*/ {
    //     // headers: { topic: 'photo'},
    //     headers: { topic: 'photo_1234567890', group: 'photo' },
    //     body: { a: i, b: "'https://anh-duong-photo/1_528527" + i + '.jpg' }
    // }, err => {
    //     if (err) console.log('send.err =>', err);
    // });
    
    ExchangeFactory.sendEx({
        topic: 'photo', 
        channel_id: 'test', 
        data: { a: i, b: "'https://anh-duong-photo/1_528527" + i + '.jpg' }
    }, err => {
        if (err) console.log('send.err =>', err);
    });
}
1.0.2

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago