Realtime Messaging SDK for JavaScript Reactive Extensions (RxJS)
npm install realtime-rxjsIf 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.
This project can be easily used within Angular2 and Ionic2 apps.
``bash`
npm install realtime-rxjs --save
`javascript
import * as RealtimeRx from 'realtime-rxjs';
import { Observable } from 'rxjs/Rx';
...
// Instantiates a new Realtime client connection
const rxConnection = new RealtimeRx.ObservableConnection();
// Sets the connection metadata (optional)
rxConnection.setConnectionMetadata("Realtime RxJs example");
// Establishes the connection to the Realtime server
rxConnection.connect("YOUR_APP_KEY", "token");
// Observe channel and subscribe to receive messages
rxConnection.observeChannel("myChannel")
.subscribe((message) => {
console.log("Received message: " + message);
});
// Send a message
rxConnection.send("myChannel", "This is the message ...");
`
Note that you don't need to manage the connection state yourself (e.g. waiting for the onConnected event before subscribing a channekl) as it's performed internally (subscriptions and sends will be pending until the underlying Realtime connection is ready).
``
rxConnection.connect("YOUR_APP_KEY", "token");
``
rxConnection.observeConnection()
.subscribe((event) => {
console.log("Realtime connection event:", event);
});observeChannel(channel: string): Observable<string>
Returns an Observable for a Realtime channel. Each message received on the channel will emit a next event for each subscribed observer.
``
rxConnection.observeChannel("myChannel")
.subscribe((message) => {
console.log("Received message: " + message);
});
Note: the underlying Realtime channel unsubscription is performed automatically when there are no more observers subscribing the channel.
but uses other subscription options like buffered messages and filters.`
rxConnection.observeChannelWithOptions("myChannel", "mySubscriberId")
.subscribe((m) => {
console.log("Received message: " + m.message);
console.log("Message id: " + m.seqId);
});
` The default unsubscribe behaviour is the same as observeChannel (unsubscribe from the channel when there no more active channel observers), but passing
autoUnsubscribe = false will not perform unsubscribes at all (useful for buffered subscriptions).send(channel: string, message: string): void
Sends a message to the given channel using the "at-most-once" delivery contract.`
rxConnection.send("myChannel", JSON.stringify({ foo: "bar" }));
`publish(channel: string, message: string, ttl: number): Observable<string>
Publishes a message to the given channel using the "at-least-once" delivery contract.`
rxConnection.publish("myChannel", JSON.stringify({ foo: "bar" }), 60)
.subscribe((seqId) => {
console.log("Message published with seqId: " + seqId);
},
(error) => console.log("Error publishing: " + error));
`Note: Don't forget to subscribe to get the publish result otherwise the message publish won't be performed at all.
disconnect(): void
Disconnects from the Realtime server. `
rxConnection.disconnect();
``