Empowering scalable, real-time web apps in Node.js
Table of Contents:
- Usage: Server-Side Module
- Usage: Client-Side module
- Demo App
- Platform Support
Socket.io powers real-time client updates on the front-end, while Node clusters and event emitters in conjunction with a custom messaging queue process highly concurrent and asynchronous operations on the back-end.
We use a messaging queue, called razorframe, that intercepts incoming user interactions over a two-way socket channel. Those interactions are held in the queue only as long as the server needs before dequeuing. The dequeuing process then triggers an event that both updates the client UI and launches a back-end process such as a database write.
Our tests have shown this process keeps the client UI updating in sub 100ms "real-time" fashion at scale while maintaining accurate database writes.
$ npm i --save razorframe
How to Use
Hosted Redis server:
We have removed the hosted Redis server originally provided during initial rollout. In order to leverage concurrency with razorframe and ensure server -> client communication, be sure to instantiate a local or hosted Redis server for your application.
You can store your Redis reference in an environment variable, or fall back to a locally hosted instance (see below):
const REDIS_URL = processenvREDIS_URL || host: 'localhost' port: 6379
(1) Require razorframe
rzConfig object to set up server processes by declaring:
rzConfig.port: port where your server is listening.
rzConfig.cluster: true or false depending on whether you want to enable Node clusters.
(Even though our config automatically accounts for 1 process if not specified, you'll still get better performance if you turn off Node clusters if you know you won't be using more than one CPU.)
dbConfig object to define your back-end callbacks
dbConfig.write: 'create' function for database.
dbConfig.show: 'read' function for database.
dbConfig.update: 'update' function for database.
dbConfig.delete: 'delete' function for databse.
(4) Initialize razorframe while passing in http (for your server) and the configurations
const rz = ;const rzConfig =port: processenvPORT || 3000cluster: true;const dbConfig =write: addToDbshow: showAllupdate: nulldelete: null;rz;
Import 2 libraries: socket.io and razorframe into your HTML.
Grab the client-side import file from our website razorfra.me or use the hosted link below:
Contains 2 methods:
rz.publish- publishes a data payload to a particular event and specifies a back-end callback
- contents: message data
- function name (as a string): a back-end operation you want to perform as defined in dbConfig.
- event name: name the event you can then subscribe to.
rz.subscribe- listens for an event coming from the server
- event name: the event you want to listen for.
- callback function: any function you want to call on the payload from the event.
Razorframe enables error handling on the back-end if your database fails to query.
Within the error callback on your database controller, use the method:
if err rz;
where 'MSG' is the task being sent to the database and the second argument, in this case '2', specifies the number of attempts to do the query. Razorframe will re-enqueue the task 'n' number of times with a default of 2 total attempts. If the event fails to query after all attempts, a message is sent to the user that enqueued the event that the event has failed to write and will be dropped.
Check out our demo app for more usage examples at: RZ-Demo
Click this Link to leave feeback. We want to hear from you! ⚡️
Tested in Chrome 55 & Node 6/7.
GitHub Issues: https://github.com/team-emt/razorframe/issues
❤️ Contributions welcome!
Please see out GitHub repo at: https://github.com/team-emt/razorframe