moleculerjs / moleculer-channels

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

Custom sendMethodName is not working, still defaulting to sendToChannel #61

Closed ujwal-setlur closed 1 year ago

ujwal-setlur commented 1 year ago

Prerequisites

Please answer the following questions for yourself before submitting an issue.

Current Behavior

Specifying sendMethodName in the options is not working. There is no method with the specified name found. Instead, the method is still called the default sendToChannel.

Expected Behavior

The broker should have a method with the name specified in the sendMethodName option.

Failure Information

Error is:

broker.sendChannelMessage is not a function

Steps to Reproduce

Set up middleware:

const mw = ChannelMiddleware({
  adapter: {
    type: 'Fake',
    sendMethodName: 'sendChannelMessage'
  }
});

Send a message:

await broker.sendChannelMessage('typedService.channel-event-1');

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

broker.sendChannelMessage is not a function
AndreMaz commented 1 year ago

Can you please provide a full repro example?

This example is working fine https://github.com/moleculerjs/moleculer-channels/blob/master/examples/multi-adapter/index.js

This one also seems to be ok.

"use strict";

const { ServiceBroker } = require("moleculer");
const ChannelsMiddleware = require("../../").Middleware;

let c = 1;

// Create broker
const broker = new ServiceBroker({
    namespace: "uat",
    logLevel: {
        CHANNELS: "debug",
        "**": "info"
    },
    middlewares: [
        ChannelsMiddleware({
            adapter: process.env.ADAPTER || "Fake",
            sendMethodName: "sendToAnotherChanel"
        })
    ],
    replCommands: [
        {
            command: "publish",
            alias: ["p"],
            async action(broker, args) {
                const { options } = args;
                //console.log(options);
                await broker.sendToAnotherChanel(
                    "my.first.topic",
                    {
                        id: 2,
                        name: "Jane Doe",
                        status: false,
                        count: ++c,
                        pid: process.pid
                    },
                    { key: "" + c, headers: { a: "something" } }
                );
            }
        }
    ]
});

broker.createService({
    name: "posts",
    version: 1,
    channels: {
        async "my.first.topic"(msg, raw) {
            this.logger.info("[POSTS] Channel One msg received", msg, raw.key, raw.headers);
        }
    }
});

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

        //await Promise.delay(1000);
        console.log("Publish 'my.first.topic' message...");
        await broker.sendToAnotherChanel("my.first.topic", {
            id: 1,
            name: "John Doe",
            status: true,
            count: c,
            pid: process.pid
        });
    })
    .catch(err => {
        broker.logger.error(err);
        broker.stop();
    });
ujwal-setlur commented 1 year ago

Duh, my bad! I had the config wrong. I had it as:

const mw = ChannelMiddleware({
  adapter: {
    type: 'Fake',
    sendMethodName: 'sendChannelMessage' // WRONG PLACE!!
  }
});

It should be

const mw = ChannelMiddleware({
  adapter: {
    type: 'Fake',
  },
  sendMethodName: 'sendChannelMessage'
});

Sorry!