{"size": 1234, "ext": "js", "max_stars_repo_path": "services/redis.sub.js", "max_stars_repo_name": "numerica-ideas/ni-microservice-nodejs", "max_stars_repo_stars_event_min_datetime": "2021-05-26T08:43:44.000Z", "max_stars_repo_stars_event_max_datetime": "2021-05-26T08:43:44.000Z", "max_issues_repo_path": "services/redis.sub.js", "max_issues_repo_name": "numerica-ideas/ni-microservice-nodejs", "max_issues_repo_issues_event_min_datetime": "2021-05-28T19:05:01.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-03T06:11:52.000Z", "max_forks_repo_path": "services/redis.sub.js", "max_forks_repo_name": "numerica-ideas/ni-microservice-nodejs", "max_forks_repo_forks_event_min_datetime": "2021-05-27T04:12:14.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-27T04:12:14.000Z"}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 30.0975609756, "max_line_length": 112, "alphanum_fraction": 0.6158833063}
/**
 * NI Redis sub service: for lambda (cloud function) deployment.
 * Useful to communicate (receive) between microservices.
 * @since 1.5.0
 * @author dassiorleando
 */
const Util = require('./util');
const constant = require('../config/constant');
const redisClient = require('../lib/redis')(true);

/**
 * Ingesting incoming App Events to save.
 * Coming from different sources (microservices).
 */
exports.ingestingEvents = function (callback = Util.noop) {
    redisClient.on('message', function (channel, data) {
        console.log('New event received');
        console.log('Microservice Events ---> Message: ' + data + ' on channel: ' + channel + ' just arrived!');

        // const event = JSON.parse(data);
        if (channel === constant.EVENTS[0]) {
            callback(null, 'Filter policy A processed.');
        } else if (channel === constant.EVENTS[1]) {
            callback(null, 'Filter policy B processed.');
        } else {
            console.error('Unknown filter policy!');
        }
    });
}

/**
 * Subscribes to events
 * @returns {void}
 */
exports.subscribing = function () {
    const events = constant.EVENTS || [];
    events.forEach(event => {
        redisClient.subscribe(event);
    });
}
