PubNub: how to correctly write all posted posts to my db

What is the correct way to log each published message and save it on the db server.

There are two options that I can think of:

  • Using the PubNub Function After posting an event and forwarding the message to a dedicated registration channel. the server will subscribe to the channel and save the received messages in db. Here another question arises: when I forward a message to another channel in the PubNub function, does it also call the PubNub function?
  • use PubNub XHR request / response function and call the shutdown API on my server with the posted message and save it in db

What are the best practices regarding performance and cost?

+7
pubnub
source share
1 answer

Save PubNub Messages to Your Personal Database

We wrote an article that talks about the correct way to register JSON messages in a private database .

There are many approaches so far. One is the best. Using PubNub Features You will reliably store messages in your database asynchronously . Using the OnAfter Publishing Event . Your database must be accessible through a secure HTTPS endpoint.

PubNub does not index your posts using FTS indexing ; at the time of writing. You might want to index full-text searches using your database, or use an API provider, such as https://www.algolia.com/, for full-text searches.

The data is valuable. AI and ML let you get an idea of ​​your data using Tensorflow . You might want to run data analysis on the contents of the message. Using EMR / Hadoop or other big data analytics software.

You will use PubNub functions to easily store messages asynchronously on your database system by following these steps:

PubNub Save Message to Database

Getting started is easy. Assuming you already have a stream of messages published to the PubNub data feed. Follow these simple steps. Successfully, you will create a real-time function that will run on every publication event.

  1. Gather a list of channels. You may be interested in ALL channels * .
  2. In your account control panel, find your application and press " Options . "
  3. Create a new module for the specified API key set.
  4. Create a new OnAfter event handler on channel * .
  5. Use the following code example to store your messages asynchronously in your database.
  6. Be sure to change the URL / parameters to suit your needs.

HTTPS asynchronous message save function

 // Request Handler export default request => { return log(request).then( () => request.ok() ); } // Async Logging/Save function log( data, retry=3 ) { const xhr = require('xhr'); const post = { method : "POST", body : request.message }; const url = "https://my.company.com/save"; // <-- CHANGE // save message asynchronously return xhr.fetch( url, post ).then( serverResponse => { // Save Success! }).catch( err => { // Retry if (retry > 0) log( data, --retry ); }); } 
+9
source share

All Articles