Best Practice | Backend as a Service Platform

Message:

Best Practice (4 posts)

In this article, we’ll talk about Backendless publish-subscribe messaging. One of the frequent questions we receive is: ‘How to get messages long after they are published.’ The default mechanism in Backendless keeps messages in the published channel for a short period of time only (around a minute). This becomes a problem if subscriber needs to have access to the messages after that time period has passed. This article describes an approach for storing published messages in Backendless database in order to keep published messages accessible even when they are no longer present in a messaging channel.

Publish-subscribe is a Backendless messaging pattern. The main idea here is to exchange data between a publisher* and subscriber** within a messaging channel***.

publisher – a program using the Publishing API to send messages to a channel.
** subscriber – a program using the Subscription API to receive messages from a channel.
*** channel – a logical intermediary “transporting” the messages.

In order to keep messages accessible for an infinite period of time – you can save messages into the Backendless database right after they were published. To accomplish this, we’ll need to combine Data service API and the afterPublish event handler which can be added to the Business Logic tab of your Backendless application.

handlerConfig 1024x565 - How to save published messages in the database

Once it’s added, just download the afterPublish handler code from ‘Download’ menu (you can select JavaScript or Java). Then open the generated project in any IDE and add the code to store published messages in a Backendless data table.

Alternatively, the afterPublish event handler can be added and deployed to cloud without any coding at all using Codeless business logic. Here are the steps of how it can be done:
1. Add new Codeless event handler Selection 143 1024x519 - How to save published messages in the database2. When the handler is saved, Codeless designer will be opened. Add the following blocks to your Codeless logic. When done – click the ‘DEPLOY MODEL’ button.
Selection 144 1024x535 - How to save published messages in the database
Voila!

To avoid nulls, every message should be published with a not null value for publisherId :

For this scenario we’ll need to create a data table (Data > APP TABLES in your Backendless application) called ‘ChatHistory’ and define the following schema in it:

  • Column publisher of type String 
  • Column messageData of type String 

dataTable 1024x557 - How to save published messages in the database

Once code for the handler is added, deploy the handler code to cloud using the following command in your IDE terminal:
./bin/Deploy.sh
Since the handler is deployed, every new message published to chatRoom channel will be stored in a dedicated data table and will be accessible at any required moment by calling an API to retrieve data from a table:

You will receive the following output:

If you’re starting an Android project with Backendless and import our SDK library from Maven, please pay attention to the version number of the library. We have published a beta version of the 4.0 SDK into Maven central. When referencing Backendless in Android Studio, version 4 is the default one to popup. Unless you’re building with Backendless version 4 (which will be the default backend in the Cloud very soon), make sure to reference version 3.0.25 of the library as shown in the screenshots below:

Continue reading

This is a question we get very often:

“how can I avoid rejected API calls when my app generates more calls than allowed by the plan’s limit?”

To give you the context of the question – all Backendless plans have a limit of API calls/minute. With the free plan the limit is 300, the Cloud 9 plan provides 600 and the Cloud 99 plan offers 1200 API calls/minute. With the Cloud9/99 plans, the limit can be increased by purchasing function packs which add 600 API calls/minute for each purchased pack. The backend monitors and enforces the limit for each minute, that is if you are on the free plan, Backendless will process the first 300 API calls for any given minute and all other calls above the limit during that minute will be rejected. When the volume of the incoming calls reaches 80%, 90% and 100% of the limit, the system will generate and send out an email informing you that a threshold has been reached.

To avoid rejected calls it is important to monitor performance of your app using Backendless console and stay on top of the notification emails. Backendless Console provides a chart of API calls/minute on the Manage > Analytics > Performance screen. The red line shows the current limit:

api calls minute - API call/minute limit and how to avoid it

As you can see in the image above, the current limit for the app is 1800 API calls/minute and the app’s traffic stays right below the red line.

When you get a notification about the 80/90% threshold, we recommend analyzing the trend of the incoming calls and assessing whether the limit should be increased. It may be a one off occurrence of the traffic spike or a consistent growth of the volume of calls.

 

Posted in Best Practice

replyto 300x176 - Hear back from you app users - configure the "from" email address

The backend for your Backendless app sends out emails for the following events:

  1. When a user creates an account for your app
  2. When a user requests password recovery
  3. When a user logs in for the first time

The default email address used by Backendless is develop@backendless.com, which is an automated account – we do not actively monitor incoming emails there. If you do not change the email address in your Backendless account and a user replies back to any of the emails listed above, the response goes back to the inbox for our automated account.

We see a lot of applications which are published to app stores where email address is not changed. As a result, you are missing direct communication received from your users. To avoid this, it is recommended to change the email address per the instructions in our documentation.

Posted in Best Practice
Find us in facebook