old Rails and php programmer that is new to js, tyring something and feel I am missing something pretty basic. I have a node server running a small webapp, express, ejs, jquery as well as alpinejs. (very exp with jquery, but learning alpinejs). I have a mqtt server running and can manipulate a couple of topics via my phone, and I can even see them in the node console (code below)
what I fail to understand is how to get, or maybe "bind" the subscription to a variable that I can "pass" into the express app so I can just show the mqtt message in the html. Im currently just trying to set things like fridgeFan = true
or somehthing. My idea, I think, is to have mqtt update the alpinejs variable, hence updating the page, then viceversa...
Can someone point me in the general good direction of what im attemtping?
options={ clientId:"node-admin", clean:true, }; const express = require('express'); const app = express(); const mqtt = require('mqtt') const client = mqtt.connect('mqtt://192.168.10.66', options) client.on("connect",function(){ console.log("connected"); }); client.on('message',function(topic, message, packet){ console.log("message is "+ message); console.log("topic is "+ topic); // assume id like to bind topic and message to // some variable that I can pass or have access to // in the res.render calls below?? // couple of things I was trying //document.getElementById('mqtt_div').innerHTML += topic; //document.querySelector('[x-data]').__x.getUnobservedData().fridgeFan = message; }); client.subscribe("sensors/sensor1",{qos:1}); app.set('view engine', 'ejs'); app.use(express.static(__dirname + '/public')); app.get('/', (req, res) => { res.render('index', { title: 'Homepage' }); });
with the above code, I can publish to "sensors/sensor1" via a phone app, I can see it in the node console, of course not the browser console.
How can I bind the "message" to a alpinejs variable so it will update?
OR do I go a different direction and put the client.on calls elsewhere, say the ejs file? in my testings, the ejs files dont know about require mqtt
没有评论:
发表评论