IN THIS ARTICLE

    Subscribe to Our Newsletter

    Stay updated with the latest on web, mobile, and IoT, delivered weekly.
    Thanks for subscribing!

    Thanks for subscribing!

    Get ready for some great content.

    We’re pleased to announce the release of rltm.js, a universal API for realtime communication. rltm.js allows you to build realtime apps with one codebase and easily switch between realtime messaging backends. Thinking of rltm.js as a realtime protocol adapter that ensures interoperability between clients and realtime 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 realtime. It works for both front-end web and Node.js. All of this information is available from generic methods and you can switch between realtime services with one small config.

    Why rltm.js?

    Most of our customers began by implementing an open source realtime solution themselves. As they grow, they realized how difficult and costly it is to scale and support their own realtime 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 realtime communities out there.

    The first framework we’ll be supporting is Socket.IO, one of the most popular open source realtime 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 realtime 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 realtime messaging APIs to rltm.js, with the goal of eventually making it a completely uniform API for realtime. Any feedback or questions, reach out to me at @sw1tch.

    Resources
    Resources

    Building a HIPAA-compliant App

    Everything You Need to Know About Developing and Scaling a HIPAA-compliant App
    Download Now
    Building a HIPAA-compliant App