News

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

3 min read Michael Carroll on Apr 19, 2017
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

rltm.js is an opensource uniform API for real-time messaging, allowing you to easily swap between Socket.IO and PubNub for bidirectional event-based communication.

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 history, 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.

More from PubNub

NPP and HIPAA: Notice of Privacy Practices Definition
Healthcare CategoryJan 6, 20235 min read

NPP and HIPAA: Notice of Privacy Practices Definition

A Notice of Privacy Practices (NPP) is one of the requirements of HIPAA and helps patients understand their personal data rights.

Michael Carroll

Michael Carroll

HIPAA Violation Examples
Healthcare CategoryJan 5, 20236 min read

HIPAA Violation Examples

HIPAA violations can be financially expensive and devastating to a brand. Examine some examples of HIPAA violations, and learn...

Michael Carroll

Michael Carroll

HIPAA Technical Safeguards: How To Protect Sensitive Data
Healthcare CategoryJan 5, 20236 min read

HIPAA Technical Safeguards: How To Protect Sensitive Data

HIPAA covered entities must follow the five technical safeguards to achieve HIPAA compliance and prevent data corruption.

Michael Carroll

Michael Carroll