Skip to content

Instantly share code, notes, and snippets.

@revant
Last active June 20, 2024 13:36
Show Gist options
  • Save revant/fe97d16616c73d2d0d22a4cebb15f835 to your computer and use it in GitHub Desktop.
Save revant/fe97d16616c73d2d0d22a4cebb15f835 to your computer and use it in GitHub Desktop.
MQTT Sub nodejs
const mqtt = require("mqtt");
const client = mqtt.connect("mqtt://event-bus:1883", {
clientId: require('crypto').randomUUID(),
clean: true,
connectTimeout: 4000,
username: 'emqx',
password: 'public',
reconnectPeriod: 1000,
});
client.on('connect', () => {
console.log('Connected');
client.subscribe("+", () => {
client.on('message', (topic, message) => {
console.log(`Received message "${message.toString()}" on topic "${topic}"`)
})
});
})
const net = require('net');
const server = net
.createServer(socket => {
socket.on('data', data => {
console.log(data.toString());
});
})
.on('error', err => {
throw err;
});
server.listen(8000, '0.0.0.0', undefined, () => {
console.log('TCP server is listening on 0.0.0.0:8000');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment