question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[bug] unable to publish with in memory mqemitter after some mqtt clients connected

See original GitHub issue

System Information

  • Aedes: 0.44.0
  • NodeJS: 12.13.1
  • OS: Ubuntu 18.04
  • Arch: x86_64

Describe the bug

Unable to publish after clients connected and publish some messages while using default in memory mqemitter.

To Reproduce

Default mqemitter concurrency number is 100 in aedes so it happened when more clients connected, it’s easy to reproduce it when concurrency set to 2 .

Steps to reproduce the behavior:

  1. $ git clone git@github.com:JaosnHsieh/aedes-unable-to-publish.git
  2. $ npm install
  3. $ npm run server
  4. $ npm run client

Expected behavior

No matter what concurrency number set to aedes, it should be able to publish message.

Additional context

I expect this._messageQueue length in mqemitter should decrease as time goes but it doesn’t.

Current work around I just change concurrency number to 10,000 so it always skip this condition.

I saw mqemitter exposed message queue length but didn’t see any code in aedes check this value.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JaosnHsiehcommented, Oct 28, 2020

It’s a very basic functionality I suggest to check your code, the error is there, if your bug would be true no one could use aedes

it doesn’t happen too often due to default concurrency 100 which able to handle quite a lot of messages. Set to 2 for easier reproduce only.

Couldn’t see errors either on broker side or mqtt.js client side.

server.js


const Socket = require('net');
const Http = require('http');
const WSServer = require('websocket-stream');
const mqemitter = require('./mqemitter');
// mqemitter options same as aedes default but added console.log to this._messageQueue.length
const mq = mqemitter({
  concurrency: 2,
  matchEmptyLevels: true, // [MQTT-4.7.1-3]
});

const aedes = require('aedes')({
  mq,
});
const MQTT_PORT = 1800;
const WS_PORT = 3030;

aedes.on('publish', (publishPacket, client) => {
  console.log(`$ on publish`, publishPacket.topic, publishPacket.payload);
});

aedes.on('subscribe', (subscriptions, client) => {
  console.log(`$on subscribe from ${client.id}`, subscriptions);
  const topic = `test/${client.id}`;
  aedes.publish(
    {
      cmd: 'publish',
      qos: 0,
      topic: topic,
      payload: `${Math.random()}`,
      retain: false,
    },
    () => {},
  );
});

aedes.on('clientError', (...args) => {
  console.log(`$ clientError`, args);
});

aedes.on('connectionError', (...args) => {
  console.log(`$ connectionError`, args);
});

serverInit();

function serverInit() {
  const server = Socket.createServer(aedes.handle);
  const httpServer = Http.createServer();
  const ws = WSServer;

  server.listen(MQTT_PORT, () => {
    console.log(`MQTT Server is on port ${MQTT_PORT}`);
  });

  ws.createServer({ server: httpServer }, aedes.handle);

  httpServer.listen(WS_PORT, () => {
    console.log(`Websocket server is on ${WS_PORT}`);
  });
}

client.js

const mqtt = require('mqtt');

const MQTT_URL = `ws://localhost:3030`;
const clientNumber = 10;

go();

function go(number) {
  // let count = 0;
  for (let i = 0; i < (number || clientNumber); ++i) {
    const random6 = `${Math.random()}`.slice(2, 8);
    const randomId = `${random6}`;
    const client = mqtt.connect(MQTT_URL, {
      clientId: `${randomId}`,
      username: `${randomId}-username`,
      password: `${randomId}-password`,
    });
    client.on('connect', (connack) => {
      const topic = `test/${randomId}`;
      client.subscribe(topic, { qos: 0 }, function (err, granted) {
        if (err) {
          console.log('$ subscribe err', err);
        }
        console.log(`$ subscribed to ${topic}`);
      });
    });
    let isReceivedMsg = false;
    client.on('message', function (topic, payload) {
      isReceivedMsg = true;
      console.log(`$ on message`, topic, payload);
      go(1);
    });
    client.on('error', (...args) => {
      console.log(`$ error`, args);
    });
    setTimeout(() => {
      if (isReceivedMsg === false) {
        console.log(`$ didn't receive message, unable to publish`);
      }
    }, 2000);
  }
}

Share with someone who also encountered same issue.

0reactions
robertsLandocommented, Oct 19, 2021

Closing as dup of #666

Read more comments on GitHub >

github_iconTop Results From Across the Web

IT17751: MQTT server fails to publish LWT messages to ... - IBM
MQTT server fails to publish a 'last will and testament' (LWT) message to subscribers when client loses connectivity.
Read more >
Memory allocation problem after successive MQTT ...
After repeated mqtt connection-disconnection processes,I have the problem of code -12 and it is impossible to connect with the MQTT broker ...
Read more >
Why MQTT generates the "Operator new out of memory" failure?
Hi guys, I am facing a problem with the MQTT protocol. After publishing several messages the program stops the execution and prints the...
Read more >
How to build your own MQTT broker in Nest.js
Invoked when server receives a valid CONNECT packet. authenticate, Invoked after preConnect . authorizePublish, publish LWT to all online ...
Read more >
MQTT Retained Messages Explained - Steve's internet Guide
If the publishing client dies ( say due to a power issue or memory leak), I'd like the client to reboot and then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found