moleculerjs / moleculer-channels

Reliable messages for Moleculer services via external queue/channel/topic.
MIT License
73 stars 15 forks source link

not found sendToChannel in ServiceBroker #27

Closed devalexandre closed 2 years ago

devalexandre commented 2 years ago

not exist method sendToChannel in ServiceBroker

https://github.com/moleculerjs/moleculer-channels/blob/master/examples/simple/index.js#L120

Reproduce code snippet

"use strict";

const { ServiceBroker } = require("moleculer");
const { MoleculerError } = require("moleculer").Errors;

let c = 1;

// Create broker
const serviceBroker = new ServiceBroker({
    logLevel: {
        CHANNELS: "debug",
        "**": "info"
    },
    replCommands: [
        {
            command: "publish",
            alias: ["p"],
            async action(broker, args) {
                const { options } = args;
                //console.log(options);
                await broker.sendToChannel(
                    "payment.processed",
                    {
                        id: 2,
                        name: "Jane Doe",
                        status: false,
                        count: ++c,
                        pid: process.pid
                    }
                );
            }
        }
    ]
});

serviceBroker.createService({
    name: "payments",

    actions: {
        /*...*/
    },

    channels: {
        "payment.processed": {
            async handler(payload) {
                console.log(`Paylod is ${JSON.stringify(payload)}`);
                // Do something with the payload
                // You should throw error if you want to NACK the message processing.
            }
        }
    },

    methods: {
        /*...*/
    }
});

serviceBroker
    .start()
    .then(async () => {
        serviceBroker.repl();

        console.log("Publish 'payment.processed' message...");

        await serviceBroker.sendToChannel(
            "payment.processed",
            {
                id: 2,
                name: "Jane Doe",
                status: false,
                count: ++c,
                pid: process.pid
            }
        );
        await Promise.delay(5000);
    })
    .catch(err => {
        serviceBroker.logger.error(err);
        serviceBroker.stop();
    });

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

Failure Logs

TypeError: serviceBroker.sendToChannel is not a function
    at /home/alexandre/opnsource/artigos/moleculer/channels/service.js:65:29
intech commented 2 years ago

moleculer-channels is middleware

devalexandre commented 2 years ago

I Register like middlware

2021-10-21_20-55

devalexandre commented 2 years ago

When use without moleculer-rpl need input config in file in construct

    middlewares: [
        ChannelsMiddleware({
            adapter: process.env.ADAPTER || "redis://localhost:6379"
        })
    ],