News

Announcing rltm.js, a Universal API for Real-time Messaging

3 min read Michael Carroll on Apr 19, 2017

We’re pleased to announce the release of rltm.js, a universal API for real-time communication. rltm.js allows you to build real-time apps with one codebase and easily switch between real-time messaging backends. Thinking of rltm.js as a real-time protocol adapter that ensures interoperability between clients and real-time messaging APIs and services.

rltm.js provides a number of handy methods for messages, rooms, users, message persistence, and finding out who’s online and offline in real time. It works for both front-end web and Node.js. All of this information is available from generic methods and you can switch between real-time services with one small config.

Why rltm.js?

Why rltmMost of our customers began by implementing an open source real-time solution themselves. As they grow, they realized how difficult and costly it is to scale and support their own real-time network. rltm.js is here to support open source implementations and give developers an easy way to transition to our service when they’re ready to scale.

At PubNub, we write a ton of open source libraries, examples and tutorials. rltm.js allows us to share this work, and our service, with all of the other real-time communities out there.

The first framework we’ll be supporting is Socket.IO, one of the most popular open source real-time frameworks out there. Besides the library itself, we’ve also updated our Angular chat plugin to work with rltm.js. Now that plugin supports Socket.IO or PubNub.

How rltm.js Works

Let’s dive into how rltm.js actually works, and how to get started. For an explanation of each of the rltm.js methods, check out this page. Otherwise, here’s how to get started.

First, install the library with npm or bower.

npm install rltm --save
bower install rltm --save

Then include library in HTML or in your Node.js app.

<script src="./bower_components/web/rltm.js"></script>
const rltm = require('rltm');

Then, configure the rltm library in your javascript code. Both the NodeJS and web libraries are configured with the rltm variable.

let user = rltm({
    service: 'pubnub',
    config: {
        // ...
    }
});

 

  • service is the name of the real-time service to use (pubnub or socketio)
  • config is a Javascript object with a config for that service.

To use PubNub, supply your publish and subscribe keys from your account. If you don’t have your publish/subscribe keys, sign up for a PubNub account and get your keys in the PubNub Admin Portal.

let user = rltm({
    service: 'pubnub', 
    config: {
        publishKey: 'YOUR_PUBNUB_PUBLISH_KEY',
        subscribeKey: 'YOUR_PUBNUB_SUBSCRIBE_KEY'
    }
});

To use Socket.IO, run the Socket.IO server and supply your Socket.IO endpoint.

node ./socket.io-server.js

Then you can configure rltm to look for the server at that endpoint.

let user = rltm({
    service: 'socketio', 
    config: {
        endpoint: 'http://localhost:9000'
    }
});

Then, you can connect to a chatroom using the join method.

room = user.join('room-name');

Now you can subscribe to messages for that room.

room.on('message', (uuid, data) => {
    console.log('message received from uuid', uuid, 'with data', data);
});

To publish a message to the room, just call room.message().

room.message({hello: world}).then(() => {
    console.log('message published');
});

 

The subscribe code above will fire.

You can also get a list of who’s in the room by calling the here() endpoint.

room.here().then((users) => {
    console.log('users online', users);
});

This will return an object of all connected users.

{ 
    uuid1: {
        username: 'ianjennings'
    },
    uuid2: {
        username: 'stephenblum'
    }
}

Wrapping Up

We’ll continue to add more open source real-time messaging APIs to rltm.js, with the goal of eventually making it a completely uniform API for real-time. Any feedback or questions, reach out to me at @PubNub.

0