JavaScript API Reference0.106.1

Overview

Now4real allows you to add a live chat to every page of your website (pagechat) and to show real-time counters, maps, and rankings of how many people are on every page (page presence and public real-time analytics).

To integrate Now4real into a site, you can either use our ready-made widget or create your own front end with this JavaScript API. The back end is provided as a SaaS platform (Now4real cloud).

In a nutshell, the Now4real API enables you to:

  • Listen to subjects (like counters, rankings, chats, status, etc.) via the subscribe method
  • Send messages to a pagechat via the post method

Getting Started

To get started simply paste the code below into your pages, preferably before the </body> tag:

<script type="text/javascript">
  window.now4real = window.now4real || {};
  now4real.config = {
    target: 'api'
  };
  now4real.onload = function () {
    console.log('Now4real v.' + this.version + ' is ready! Now you can start to interact with library methods...');
  };
  now4real.onerror = function (error) {
    console.log('Now4real has failed to load', error.code, error.message);
  };
  (function () { var n4r = document.createElement('script'); n4r.type = 'text/javascript'; n4r.async = true; n4r.src = 'https://cdn.now4real.com/now4real.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(n4r, s); })();
</script>

This will register now4real as a global variable (see the Now4real class).
The library will automatically connect to the Now4real cloud.

For the full list of configuration parameters, please check the Config object.

Subject Callbacks

To listen to subjects, you need to use the subscribe method. It takes a callback function as the second parameter, which will be called with an instance of Now4realSubjectUpdate at every subject update. You can access the subject payload through the data field.

  await now4real.subscribe(now4real.Subject.CONNECTION_STATUS, upd => {
    console.log('Now4real connection status: ' + upd.data.status);
  });

Error Handling

All the Now4real methods (except for get) are asynchronous and will return a Promise.
In case of rejection, an instance of Now4realError will be returned. You can read the code field to handle the specific error.

  now4real.post('Hello!')
    .catch(error => {
      switch (error.code) {
        case now4real.ErrorCode.NOT_AUTHENTICATED:
          console.log('You need to sign in first');
          break;
        case now4real.ErrorCode.TOO_FREQUENT:
          console.log('You are too fast!');
          break;
        default:
          console.log('Post error', error.code, error.message);
      }
    });

Browser Support

The Now4real JS API provides support for all the modern browsers and some of the older ones, across different devices such as desktops, smartphones, tablets, smart TVs, etc.

While our code is ES5 compliant, the browser should support at least Page Visibility API, Local Storage, and Cross-Origin Communication.

If you have any problems with a specific browser or device, please contact our Support team.