Best way to give front end data on client connect?


#1

I built a plugin that displays the current layer of a print job. The host handles analyzing the GCode and notifying the clients when a layer change has taken place. The issue I've run into is that I don't know how to push the layer information to the client when the client first connects to the host.

I'm currently sending updates to the client via send_plugin_message. Is there a straight forward way for the client to request an update? How does OctoPrint keep other progress metrics in sync with the client?

This is my first time using python or knockout, I may be missing some obvious stuff. Here is the repo the project if anyone is interested: https://github.com/chatrat12/layerdisplay


#2

Use a REST API endpoint and have the client either request an update via that, or alternatively allow it to query the current state at will through that. The latter is probably the cleaner version since it doesn't force push data to all clients.

Rule of thumb: REST API = on demand, socket = push.

Regarding the printer metrics in core OctoPrint, there's the history push message with a metric backlog (current state + terminal history + temperature history) that gets send to all clients after initial connect which OctoPrint's UI utilizes mostly for initial metric sync, and then updates itself via the regular updates received thereafter (current). In cases where that isn't sufficient, OctoPrint and the bundled plugins utilize exactly the same API query approach outlined above.


#3

Thanks for the reply :smiley: Can you point me the right direction on how to add a custom REST endpoint? If you can point me to a plugin that does that, I can dive though the code. I've been pouring through the documentation and can't quite figure out to implement one.


#4

Take a look at the bundled Announcement, Software Update or Plugin Manager plugins, or alternatively take a look at the BlueprintPluginMixin and/or the SimpleApiPluginMixin @ http://docs.octoprint.org/en/master/plugins/mixins.html#available-plugin-mixins


#5

Thank you for the help! :slight_smile: I had read the description for the blueprint mixin a few days ago and forgot all about it :roll_eyes: I was sleep deprived and scouring through the REST API docs. xD You are the best @foosel!