Node.js: Bulk Message Delivery as a Runtime Service

Automatically send Markdown formatted messages in bulk to app users

Message types and their delivery requirements

Introducing the Indefinite Runtime Process

The runtime service delivering new account welcome messages in one interval execution.
// `setInterval` loop executing service every 30 minutessetInterval(() => {
sendPendingMessages();
}, 1000 * 60 * 30);
// calling module service function from its `init` functioninit: async function () {      
module.exports.generateWelcomeMessages();

setInterval(async function () {
module.exports.generateWelcomeMessages();
}, interval);

}
// starting service as a background process with pm2pm2 start service.js --name 'auto-messages'

Project Setup and File Structure

Project structure diagram
// file structure of runtime servicedata/
Accounts.js
services/
auto-messages.js
templates/
welcome.js
init.js
mongo.js
package.json
...

Initialising the service with init.js

// `init.js` service entry fileconst Service_AutoMessages = require('./services/auto-messages');async function init () {  // start services
Service_AutoMessages.init();
// more services could be started here...
}
init();

Service Implementation

Template Modules

// templates/welcome.jsmodule.exports = {
generate: (name) => ({
subject: `Welcome!`,
message: `Hello ${name !== '' ? ` ${name},` : `!`}
## Thank you for signing up!If you have any queries or concerns about the app, if something does not work, or if you have [feedback](https://my-domain.com/contact) for improvements, please feel free to contact us.### From Everyone at App Team`
})
};

Data Module

// updating account metadata to prevent message duplicationaccountsWelcomeSent: async (_id) => {
await Connection.db
.collection('users')
.updateOne({
_id: _id
}, {
$set: {
'meta.sentWelcome': true,
}
});
}

Service Module

Displaying Messages in React Native

In Summary

Programmer and Author. Director @ JKRBInvestments.com. Creator of LearnChineseGrammar.com for iOS.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store