Real-time data streaming aka the problem that FeathersJS tries to solve
A modern backend API typically uses REST to send data to and from clients in a stateless manner. However, a lot of web applications now require real-time data. This applies for applications in various fields of technology, for example the telecommunication applications like WhatsApp and other messaging apps that I’ve mentioned earlier. Real-time means that a constant stream of data is created between server and client over websockets, which allows a constantly updating state of information, instead of a stateless request being made between a client and a server.
A REST application would need a poll, a service to first check if there is any new information and, when there is, update the client’s information accordingly. A real-time data solution, however, would directly react to any changes and pass that information all the way to the client. The client wouldn’t need to poll for the information. A much more effective way of always getting the most up-to-date information is enabled. This also means, that there is a need for tools that allow us to stream the data in real-time instead of constant data polling.
Entry of FeathersJS! (Medieval trumpet noises)
The functionality of FeathersJS
There are many tools providing a solution for the issue of real-time data streaming. However, FeathersJS is the one I’ve recently had the pleasure working with and I’ve also discovered some more benefits:
It abstracts away the transport of the data and allows to snap between REST and websocket (real-time) communication without having to change any of the underlying code (only config).
Built-in support for paging, searching, sorting, and filtering.
It comes with multiple database adapters for SQL and NoSQL databases allowing the change of database technologies (per service in the app) with a single line of code.
FeathersJS is a lightweight wrapper around Express.JS, leaning on the large community and online resources available for developers.
It uses code generation to mitigate boilerplate code that uses up the developers’ time which instead could be put towards implementing valuable business logic.
Talking about structure
Please note that the following explanations are incredibly brief. If you want more information, please also refer to the detailed FeathersJS documentation that I’ve linked at the end of this post.
The structure of a FeathersJS API can be roughly broken down into these parts:
Services are responsible for the entire CRUD logic of a resource and act as the controllers for the API. Developers can hook onto life cycle events to add more business logic.
Hooks are small portions of business logic that can be added to service life cycle events. They process information and then pass the result back to the application.
Events can be used as an alternative to directly binding hooks to services. You can use them to execute logic on the life cycle of a service from other places in the application like this:
Giving an example
Now, I will present a quick example of setting up a FeathersJS API and make calls to it. For this we will create an application that creates and reads TODO items (by far the most original and unique idea!).
Step 1: Install the FeathersJS CLI
The first step is to install the CLI, from which we can create the application, using this command:
npm install -g @feathersjs/cli
Step 2: Create the application
Next, we will create our API application and select the options that we would like to set the project up with:
feathers generate app
Here you can see that I selected the Socket.io option for the real-time communication as well as REST.
Step 3: Create the service
Next, we would like to create our service that will be responsible for all of the CRUD operations on the resource.
For this example app, we will create the CRUD service for the TODO objects:
feathers generate service
Step 4: Start the application
Start the appliciation with this command:
Step 5: Test the service
Lastly, I will use Postman to make calls to the API and check that things are operational.
This is an example of a POST of a basic TODO object to the API:
After a couple of POSTs, let’s retrieve our list of TODOs:
In the response you can see that it has built-in support for paging.
There are many tools out there offering you assistance with creating APIs as well as handling real-time data. FeathersJS is one option that can help you reduce boilerplate code. It also allows for more dynamic choices in terms of application transport and databases for each API, lending itself quite well to microservice architecture.