Skip to content

Instantly share code, notes, and snippets.

@alpgul
Created April 28, 2024 15:56
Show Gist options
  • Save alpgul/8b9d6214c7f28a18b0bd3d13ddeb6b6b to your computer and use it in GitHub Desktop.
Save alpgul/8b9d6214c7f28a18b0bd3d13ddeb6b6b to your computer and use it in GitHub Desktop.
MQTT
// 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