How to update html on receiving new event from core

I am creating a Node and Express website in which events are received from a spark core and then the data it sends is displayed on the site.
I am new to MVC, so I wasn’t sure how to actually display the core data on the site.

I am using the spark node module, and in my app.js, I have

//set up express routes and views above this code

spark.on('login', function() {
	console.log("logged in to spark api");
    //populateTable();
    
    //Get updates for every event from our devices
    spark.getEventStream(false, 'mine', function(data) {
        var rawData = JSON.stringify(data)
        console.log("event stream event: " + rawData + "\n");
        var data = JSON.parse(rawData);
        console.log("core id: " + data.coreid);            
    });
});
         
//Login to the spark api
spark.login({accessToken: '<accesstoken>'});

But how do I actually display my core id on the website?
I think I would have to call a POST request to one of my routes, but how do I do that from app.js?
Or is there a better way to update the html whenever a new event is received?

For really simple things, I subscribe to the event stream on the client side and just use jQuery to update the website. You can just use SSE in a script in the page to listen for events and then update the page however you wish (jQuery, angular, etc)

I have an example in one of my projects here: https://github.com/eely22/bluzGrillThermometer

Just look at the file page/index.HTML and you will see the SSE listener setup and how it uses jQuery to update the website.

That’s what I ended up doing anyway, but I still want to know how you would do it through the node module though.

Is there a particular reason? Doing it on the client side is perfectly fine.

I have done things where I catch the events in node, then use SSE to send them back out to the client. I did that here: https://github.com/eely22/spark_hose

Mainly, that was because client side SSE only let’s you filter certain ways and I wanted all events from every core. But other than specific cases like that, I would just do it in the client.

As you said before, you could use some method where the client fetches from the server, but that seems overly complex to me.

1 Like

Oh thanks for that example.

There’s no real reason why I can’t do it client side, but I was just curious about how to actually go about it through non-client side since they give us a node module.

If your main goal is to show it on the client side, and there is a script running there already, then why not use it? The node module can be used in both Node.js, as well as in the browser/client. The reason why you’d want to use it on the Node.js side could be for data storing purposes. I store incoming SSEs in a MongoDB database, from which I then retrieve them to be displayed on the client side.
If there is no processing on the server side, and your goal is to simply display it on the client side, then why bother? It will save you some HTML trips, which will make it faster, as well as save resources.

Having played with the MEAN stack for a bit, I know there are two controllers running. One on the server side, and one on the client side. These interact with each other, whereby the server sided one can change the model, and the client sided one the view. If you want data to be displayed on the client side, you’ll need to request it from the server controller by the client controller, or have the sever controller push it to the client controller, which can then in either case pass it on to the view. Depending on what your goals are, either method could be better: client or server sided.

I actually am storing the data in a mongoDB database, but I’m currently doing it through the client-side by calling a POST request.
This method is working fine for me, but I just wanted to learn how to do it on the server side.