Created
April 28, 2024 15:56
-
-
Save alpgul/8b9d6214c7f28a18b0bd3d13ddeb6b6b to your computer and use it in GitHub Desktop.
MQTT
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
// mqtt örneği | |
// mqtt paketini dahil edin | |
const mqtt = require('mqtt'); | |
// Broker'a bağlanın | |
const client = mqtt.connect('mqtt://test.mosquitto.org'); // Örnek olarak, test.mosquitto.org üzerindeki ücretsiz bir MQTT broker kullanılabilir | |
// Bağlandığında gerçekleşecek olayı dinleyin | |
client.on('connect', () => { | |
console.log('MQTT broker\'a bağlandı.'); | |
// Bir konu (topic) üzerinden mesaj yayınlayın | |
client.publish('test-topic', 'Merhaba MQTT!'); | |
}); | |
// Mesajları dinlemek için bir olay dinleyici ekleyin | |
client.on('message', (topic, message) => { | |
console.log(`Alınan mesaj: ${message.toString()} - Konu: ${topic}`); | |
}); | |
// Bir konuyu dinlemek için abone olun | |
client.subscribe('test-topic'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment