Skip to content

Instantly share code, notes, and snippets.

@ourmaninamsterdam
Last active March 10, 2023 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ourmaninamsterdam/ed858d1ab3710fa8c84ff0accf3ca9d5 to your computer and use it in GitHub Desktop.
Save ourmaninamsterdam/ed858d1ab3710fa8c84ff0accf3ca9d5 to your computer and use it in GitHub Desktop.
// index.js
require('./a');
require('./b');
// mqtt.js
class MQTT {
connect() {
const connectionId = Math.random();
console.log('connected to: ', connectionId);
this.connectionId = connectionId;
}
publish(message) {
console.log('(class) published to', message);
}
subscribe(topic){
console.log('(class) subscribed to:', topic);
}
}
const client = new MQTT();
client.connect();
module.exports = client;
// a.js
const { publish, subscribe, connectionId } = require('./mqtt');
publish('Hello from A');
subscribe('A Topic');
console.log({connectionId});
// b.js
const { publish, subscribe, connectionId } = require('./mqtt');
publish('Hello from B');
subscribe('B Topic');
console.log({connectionId});
@ourmaninamsterdam
Copy link
Author

Running index.js will give you:

connected to:  0.6625096413626022
(class) published to Hello from A
(class) subscribed to: A Topic
{ connectionId: 0.6625096413626022 }
(class) published to Hello from B
(class) subscribed to: B Topic
{ connectionId: 0.6625096413626022 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment