Jsw module life cycle

In ‘native’ node.js it is possible to keep global variables in app.js. In velo’s backend, there’s no such file.
What happens if I do the this in web module (jsw)?
I’ve tried and I see that the variable is maintained for about an hour, i.e every client that invoke the module see the same variable, after an hour the variable is cleared.
Is this the formal behavior? can I tune this? is there another way to maintain this ?

This is not formal or tuneable behavior. When web modules are called there’s a service spun up that runs for a bit and then shuts back down. If you need to store data in the backend then you’ll want to use Content Collections and use the wix-data API to store/retrieve that data.

As far as I understand from a Wix youtube about the backend arch’. The JSW is loaded into the node.js once one of its function are invoked. The node.js shut down if no one access the backend for 5 minutes. Based on the following, once node.js is up the JWS variables are kept in memory and can be accessed by the JSW code upon a consecutive calls from any web page client. I’ve run a test for that and it seems this is the way it behaves. Can u confirm on this?

Yes this is generally how it works but I would caution against relying on this behavior as it can always change in the future. It’s not a guarantee or intended to be used like this.

4 Likes

thanks,

How do u suggest to manage a connection to an external database or other service that requires an open socket? it does not make sense to open and close the connection on each request.

For external databases take a look at Integrating External Databases with Your Wix Site and see if Wix already has an integration with your provider.

Wix’s architecture isn’t built for long running processes so for this I’d say:

  • Does your service provider offer a REST API or some other way of intermittent communication? Wix can also respond to webhooks by using HTTP functions if your website is receiving requests.
  • Can you cache data on the Wix side so you’re not opening a socket for each request? This way you can send/receive them periodically in bulk.