0

I have a redisClient and configured a separate publisher and subscriber with duplicate although I am not sure I need both of them. Redis server works ok and redisClient can write/read from the server correctly. This is the code:

  const client = redis.createClient();
  redisClient = await client.on('error', err => console.log('Redis Client Error', err)).connect();

  publisher = client.duplicate();
  await publisher.connect();
  subscriber = client.duplicate();
  await subscriber.connect();

  subscriber.on('message', (channel, message) => console.log('received'));
  subscriber.subscribe('abc123');
  publisher.publish('abc123','lala');

and I get this error, the source of it starts at the line of subscriber.connect();

Uncaught TypeError TypeError: listener is not a function at _PubSub_emitPubSubMessage (c:\src\server\node_modules@redis\client\dist\lib\client\pub-sub.js:304:9) at handleMessageReply (c:\src\server\node_modules@redis\client\dist\lib\client\pub-sub.js:233:93)

1 Answer 1

1
+100

The subscriber listener should be provided as the second parameter to the subscribe function, not to the subscriber istance.

const client = redis.createClient();
redisClient = await client.on('error', err => console.log('Redis Client Error', err)).connect();

const publisher = client.duplicate();
await publisher.connect();
const subscriber = client.duplicate();
await subscriber.connect();

// subscriber.on('message', (channel, message) => console.log('received')); --> This line is useless
subscriber.subscribe('abc123', (channel, message) => console.log('received')); // --> Instead, you need to listen for messages here
publisher.publish('abc123','lala');

You can read more here

Sign up to request clarification or add additional context in comments.

1 Comment

gave you the bounty and thanks although code doesn't work because for some weird reason they decided to make the function message,channel and not the other way. still I was stuck on something else and you solved it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.