Last active
June 20, 2024 13:36
-
-
Save revant/fe97d16616c73d2d0d22a4cebb15f835 to your computer and use it in GitHub Desktop.
MQTT Sub nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"`) | |
}) | |
}); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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