Calling frontend from motoko backend

Hi,
I want to know if there’s a way to make a backend canister written with motoko to call the frontend canister (built with node.js).

What I want to do is update my frontend page in real time based on some triggers reveived by the backend. So that when some data are updated on the backend it will call the frontend (like a notification message for example…).

I had to use http calls? Or there are other ways?
Thanks.

Last time I checked web sockets were not support yet therefore I don’t think you can push from backend to frontend directly at the moment.

There was a similar question on Discord recently, for such use case I personally choose to implement some kind of cronjob features in web workers that perdiodically queries the backend.

@peterparker Do you think something like this could work? Simple http request handler canister in Motoko - Developers - Internet Computer Developer Forum (dfinity.org)

This - the http_request function - is commonly use to serve assets (html page, images, js, css etc.) on the web. It’s like the function that answer to the http request your browser do e.g. when you access your dapp with the browser on https://canister-id.ic0.app

It also don’t push data but respond to a query so it does not solve your original question in my opinion.

Out of curiosity, what technology are you using for your frontend dapp, React and JavaScript?

Maybe is usefull for me, 'cause what I really want is that a particular user do something and the frontend of another user is updated, more like a chatbot, probably I wasn’t clear on my original question.
Anyway for the frontend part I just use javascript with node.js.

Yes yes a chat is totally what I was thinking at. Don’t think this function is your solution nor that data can be pushed at the moment but again, if that’s wrong, please correct my saying.

JavaScript, well noted. Was asking if one day I ever wrote a blog post about running cronjob to query the IC in web workers. Thanks for the feedback.

1 Like

I never used it but is not OpenChat an example of this? It will be interesting to know how it works.

Good you mention it, they literally open source their repo … today :wink:

1 Like

Oh nice, I’ll check it out!

I’ve found out, even without checking the source, that if you go to OpenChat and open the developer tool on the net tab there is a query every 5 seconds.

So you were right about that is impossible for now. I really looking forward your future blog post!

1 Like

I threw together a little node.js websockets example a while back GitHub - krpeacock/websockets-example: nodejs websockets for an IC event stack canister

It’s not really any better than polling, other than potentially sending fewer requests through the actual canister

2 Likes

@lpettenella as promised, I just shared a blog post about polling the IC with web workers

P.S.: I opened a new thread in case there would be specific feedbacks

4 Likes

Thanks, when I have time I’ll try to use it!

1 Like

See also:

2 Likes

Looks like they poll from the window side through the service worker, sounds interesting. They are not open source right? I’ll be curious to see their implementation.

:wave: We’re indeed polling client-side using a library (React Query). We’re not open source yet, but we do plan to be in the future :blush:

1 Like