All Projects → pubnub → Rltm.js

pubnub / Rltm.js

Licence: other
Easily swap realtime providers with a single code base

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Rltm.js

Progress Bot
High-tech weaponized moe progress delivery bot for IRC, Discord, and web
Stars: ✭ 38 (-64.15%)
Mutual labels:  realtime, socket, socket-io
SocketIOUnity
A Wrapper for socket.io-client-csharp to work with Unity.
Stars: ✭ 69 (-34.91%)
Mutual labels:  socket, socket-io, realtime
TweetMigration
A WebGL heatmap of global Twitter activity
Stars: ✭ 42 (-60.38%)
Mutual labels:  socket, socket-io, realtime
Kotlin Firebase Group Chat
Group and OneonOne chat using firebase built in Kotlin similar to whatsapp.
Stars: ✭ 44 (-58.49%)
Mutual labels:  realtime-messaging, realtime
Ably Go
Go client library SDK for Ably realtime messaging service
Stars: ✭ 29 (-72.64%)
Mutual labels:  realtime-messaging, realtime
Realtime Newsapi
Financial News Aggregator - Real Time & Query API for Financial News
Stars: ✭ 34 (-67.92%)
Mutual labels:  realtime, socket-io
Laravel Video Chat
Laravel Video Chat using Socket.IO and WebRTC
Stars: ✭ 646 (+509.43%)
Mutual labels:  realtime, socket-io
Chatter
A chatting app using socket.io
Stars: ✭ 53 (-50%)
Mutual labels:  socket, socket-io
Engine And Editor
Streamr Core backend
Stars: ✭ 44 (-58.49%)
Mutual labels:  realtime-messaging, realtime
Vynchronize
Watch videos with friends online with the new real time video synchronization platform
Stars: ✭ 1,072 (+911.32%)
Mutual labels:  realtime, socket-io
Realtime Rails
Realtime rails support. See website for documentation:
Stars: ✭ 77 (-27.36%)
Mutual labels:  realtime, socket-io
Peeplus
python+vue3前后端分离项目
Stars: ✭ 28 (-73.58%)
Mutual labels:  socket, socket-io
Dodgem
A Simple Multiplayer Game, built with Mage Game Engine.
Stars: ✭ 12 (-88.68%)
Mutual labels:  socket, socket-io
Socketcluster Server
Minimal server module for SocketCluster
Stars: ✭ 70 (-33.96%)
Mutual labels:  realtime, socket
Socket.io Redux
Redux middleware to emit action via socket.io
Stars: ✭ 103 (-2.83%)
Mutual labels:  socket, socket-io
Clusterws
💥 Lightweight, fast and powerful framework for building scalable WebSocket applications in Node.js
Stars: ✭ 868 (+718.87%)
Mutual labels:  realtime, socket
Beaver
💨 A real time messaging system to build a scalable in-app notifications, multiplayer games, chat apps in web and mobile apps.
Stars: ✭ 1,056 (+896.23%)
Mutual labels:  realtime, socket-io
Aaronvandenberg.nl
⚛️ Web Developers portfolio build with Gatsby.js & React.js
Stars: ✭ 98 (-7.55%)
Mutual labels:  realtime, socket-io
Angular Contacts App Example
Full Stack Angular PWA example app with NgRx & NestJS
Stars: ✭ 570 (+437.74%)
Mutual labels:  socket, socket-io
Applozic Android Sdk
Android Real Time Chat & Messaging SDK
Stars: ✭ 611 (+476.42%)
Mutual labels:  realtime-messaging, realtime

Universal API for realtime services. Integrate once and easily switch between Socket.io and PubNub.

Provides handy methods for rooms, users, message history, and information about connected user.

Supported realtime services:

Setup

NPM

Install via NPM.

npm install rltm --save

Include library via require.

const rltm = require('rltm');

Web

Install via bower or NPM

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

Include library in HTML.

<script src="./bower_components/web/rltm.js"></script>

Configure

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.

PubNub or Socket.io

Set Up With Socket.io

Socket.io is an open source websocket framework. To use socket.io, you'll run your own socket.io server on the back end.

Set Up With PubNub

PubNub is a hosted realtime solution that doesn't require you to run or maintain any servers.

Usage

Users

Every user connected to rltm.js has two properties:

  1. uuid - a unique way to identify this user
  2. state - data associated with this user

You can provide these as parameters during initialization.

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

Rooms

Realtime communication happens over rooms. rooms are like chat rooms, everybody in a room receives events sent by every other user.

A user can join a room by using the join() method and supplying a room identifier. users who provide the same identifier will be able to communicate with each other.

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

This returns a room object which we can use to communicate with other users.

Join Event

A room can subscribe to the join event to find out when other users join the room.

room.on('join', (uuid, state) => {
    console.log('user with uuid', uuid, 'joined with state', state);
});

Messages

Message Event

When another user sends a message to the room, it will trigger the message event. The room can subscribe to that event with the on() method.

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

Publish

To send a message to the entire room, use the message() method. Returns a promise.

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

Online Users

Here Now

A room can get a list of other users who have in the room by using the here() method. Returns a promise.

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

Successful responses will return a object of users who are currently connected to the room. The keys are the user's uuids and the values are their current state.

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

Leave Event

A room can subscribe to the leave event to find out when a user leaves.

room.on('leave', (uuid) => {
    console.log('user with uuid', uuid, 'has left');
});

A user can manually leave a room by using the leave() method. Returns a promise.

room.leave().then(() => {
    console.log('left the room.');
});

This will fire the leave event.

Disconnect

If a user gets disconnected without leaving the room, the disconnect event will fire.

room.on('disconnect', (uuid) => {
    console.log('user with uuid', uuid, 'has disconnected');
});

Set User State

A user state can be updated at any time by using the state() method. Supply the new state as the only parameter. Return a promise.

room.state({idle: true}).then(() => {
    console.log('state set');
});

This will fire the state event which you can subscribe to with the room.on() method. When fired you will get the uuid of the user and the new state.

room.on('state', (uuid, state) => {
    console.log('user with uuid', uuid, 'was given state', state);
});

Get Old Messages

A user can retrieve previously published messages in the room by using the history() method. Returns a promise.

room.history().then((history) => {
    console.log('got array of all messages in channel', history);
});

It will return the last 100 messages as an array of objects containing the uuid and data of every message. The array is sorted newest to oldest.

[ 
    { 
        uuid: uuid2, 
        data: { 
            sentTime: '2pm',
            text: 'boy howdy' 
        } 
    }, 
    { 
        uuid: uuid1, 
        data: { 
            sentTime: '1pm',
            text: 'hello there' 
        } 
    }
]

Test

Tests are run with mocha and chai.

npm install mocha -g
npm install chai -g

Set environment variable CLIENT to test either service.

env CLIENT=pubnub mocha
env CLIENT=socketio mocha
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].