Hi,
I am trying to use the various Nodejs modules inside my template without success.
For example I would like to use the “FS” (FileSystem) to update my template when it see a data file has been updated.
I have attempted to use “requireJS” as a min.js reference in order to get the module to work but the error back is: “uncaught referenceerror: require is not defined”.
Could some help by posting a few lines on how to use FS inside a template?
Many thanks
Frank
The nodejs api (like fs) aren’t available in the browser, you’ll have to resort to web api instead. If you need local file access, the File System Api (File System API - Web APIs | MDN) might work, I’m a tad unsure whether the chromium renderer in CasparCG allows that though…
Another way you could achieve what you want, is to run a separate nodejs application that monitors your files, and then you set up a websocket connection between your template and nodejs, and send the data over that.
What sure works is local storage.
Hi didkunz, thankyou for your post, can you explain what you mean by “What sure works is local storage”. I apologise for not understanding the advice.
Oh, sorry. There is a thing in JavaScript, that allows you to store key/value pairs. They will be presided over sessions. This thing is called LocalStorage.
But for your application that is maybe not useful. It is possible to read the content of a file (text, XML or JSON) from inside a template, by using XMLHttpRequest
. But AFAIK it is not possible to read the last-updated date of a file. So you would need too pool for changes. For XMLHttpRequest
to work you need to start CasparCG Server with a command similar to this:
casparcg.exe casparcg.config --disable-web-security
What exactly is the use case? What is inside the these data-files? And have you control over the content of them?
We are taking data from a text file and populating a display with the content. The data is for auctions so it changes everytime there is a bid or the lot is sold and there is new lot. At the moment we are polling the text file every second with a loop function but we noticed that NodeJS has a Watchfile module which would be a lot simplier but we couldn’t get it to work inside the template.
We are currently looking at running a small c# programme that will listen and then hopefully update the template via a websocket (we are now learning about websockets).
My programmer has a c# history so is more comfortable about doing the eventlistner in C but we still would like to learn to do more with Java.
You can simply send a CG UPDATE command to the template from your C# program. That would maybe be much easier, than doing it with WebSocket.
I see your use case, but understanding the security model of browsers, what you want to achieve won’t be possible. Now, there’s a recently available API that might be closer to your use case, but I can’t exactly tell if it is supported by the Chrome Embedded Framework version that CasparCG is using.
Now, as has been mentioned, you could use WebSockets or send CG UPDATE
using an external program. Personally, I’d incline for WebSockets, as it can easily provide a space for more integrations, and there are plenty of libraries that will allow you to implement those. Also, it is a proven solution, as many of the most modern CG controllers do effectively use an implementation of WebSockets, like socket.io, that are compatible with CEF versions included in CasparCG as far back as CasparCG 2.2.
I have used Laravel Reverb lately, as I’m a PHP developer mainly, and it has worked fine in the applications I have developed. I’ve also done long-polling applications using Laravel that have worked just fine.
Thanks for this response, we are trying via the CG Update command first and then try other options.