Realtime Cloud Messaging Client (websocket + xhr-streaming fallback)
npm install realtime-messagingIf your application has data that needs to be updated in the user’s interface as it changes (e.g. real-time stock quotes or ever changing social news feed) Realtime Cloud Messaging is the reliable, easy, unbelievably fast, “works everywhere” solution.
We have included the source code and licence for the sockjs-client library.
If you're using Realtime on a web page, you can install the library via:
#### CDN
``html`
Usage:
`javascript
var client = RealtimeMessaging.createClient();
client.setClusterUrl("http://ortc-developers.realtime.co/server/2.1/");
client.connect('YOUR_APPKEY', 'token');
client.onConnected = function(client) {
console.log("realtime connected");
client.subscribe("channel", true, function(client, channel, message) {
console.log("Received message:", message);
});
}
`
#### Bower
`bash`
bower install realtime
and then
`html`
More about Bower at http://bower.io/
#### NPM
`bash`
npm install realtime-messaging --save
and then
`javascript
import * as RealtimeMessaging from 'realtime-messaging';
const client = RealtimeMessaging.createClient();
client.setClusterUrl("http://ortc-developers.realtime.co/server/2.1/");
client.connect('YOUR_APPKEY', 'token');
client.onConnected = (client) => {
console.log("realtime connected");
client.subscribe("channel", true, (client, channel, message) => {
console.log("Received message:", message);
});
}
`
`typescript
import {Component, OnInit} from '@angular/core';
import * as Realtime from 'realtime-messaging';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor() {
const realtime = Realtime.createClient();
realtime.setClusterUrl("https://ortc-developers.realtime.co/server/ssl/2.1/");
realtime.connect("YOUR_APPKEY", "SomeSecurityToken");
realtime.onConnected = (client: Realtime.Client) => {
console.log("connected");
// subscribe a channel to receive message
client.subscribe("myChannel", true, this.onMessage);
}
}
// A new message was received
onMessage(client: Realtime.Client, channel: string, message: string) {
console.log("Received message:", message);
}
}
``