Where to put code that runs in parallel with the app?

I’m new to RedwoodJS (from a Django background) and hugely impressed - the documentation is great and it seems really powerful.

On to my question. I’m writing an app that allows users to update records via WhatsApp, as well as via forms. So I have some code using whatapp-web.js that runs a WhatsApp client, listens for messages and handles them. (Let’s say for the sake of argument that it allows a user to create a new blog post in the Redwood sample app via WhatsApp.)

My questions is: where within the a Redwood app should this code live?

It’s currently a standalone file, and looks bit like this example. But I would like to extend it so that the message handlers make writes to the database tables, and the code needs to start the WhatsApp client whenever the app starts.

I’m not sure where the logical place to put it would be to make sure it runs with the rest of the app, and give it access to service functions like updatePost.

Welcome @cakehoover, glad to have you! Thanks for the kind words :slight_smile:

For your question, you might check the RW file structure out: Redwood File Structure | RedwoodJS Docs

This might be something you could put in lib, but I think you will want it in the scripts folder. Scripts can access your services and libraries.

Here is some documentation on scripts: RW Script

Generates an arbitrary Node.js script in ./scripts/<name> that can be used with redwood execute command later.

Scripts have access to services and libraries used in your project. Some examples of how this can be useful:

  • create special database seed scripts for different scenarios
  • sync products and prices from your payment provider
  • running cleanup jobs on a regular basis e.g. delete stale/expired data
  • sync data between platforms e.g. email from your db to your email marketing platform

Instructions for running with rw exec here: RW Execute

Execute scripts generated by yarn redwood generate script <name> to run one-off operations, long-running jobs, or utility scripts.

I hope this gets you what you want. Please feel free to follow up if not or if you have any other thoughts.



1 Like

Thanks! That sounds great :smiley:

The last remaining part of the jigsaw puzzle is how to make sure that my custom rw exec command runs whwnever the app starts. Is there a way I can attach it to rw dev (and whatever is the equivalent startup command in production)?

Hey cake,

I don’t want to tell you bad info. Hopefully we can beg someone else to chime in with a better answer.

I am not sure on running it with rw dev, besides trying something like yarn rw dev && yarn rw exec <script>.

For updating deploy scripts: I link you to this discussion, for serverless deploy like Vercel. It might help you with that or similar options.

With baremetal deploy, you can look at this doc for help with inserting custom commands.

So, I will ask - where/how are you deploying?