项目作者: realtime-framework

项目描述 :
Realtime Messaging SDK for JavaScript Reactive Extensions (RxJS)
高级语言: TypeScript
项目地址: git://github.com/realtime-framework/RealtimeRxJS.git
创建时间: 2017-01-18T17:47:41Z
项目社区:https://github.com/realtime-framework/RealtimeRxJS

开源协议:MIT License

下载


Realtime Messaging SDK for JavaScript Reactive Extensions (RxJS)

Part of the The Realtime® Framework, Realtime Cloud Messaging (aka ORTC) is a secure, fast and highly scalable cloud-hosted Pub/Sub real-time message broker for web and mobile apps.

If 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.

Overview

This project provides Reactive Extensions for JavaScript (RxJS) bindings for the Realtime Messaging JavaScript SDK, allowing easy channel subscription as Rx Observables.

This project can be easily used within Angular2 and Ionic2 apps.

Installing with NPM

  1. npm install realtime-rxjs --save

Usage example

  1. import * as RealtimeRx from 'realtime-rxjs';
  2. import { Observable } from 'rxjs/Rx';
  3. ...
  4. // Instantiates a new Realtime client connection
  5. const rxConnection = new RealtimeRx.ObservableConnection();
  6. // Sets the connection metadata (optional)
  7. rxConnection.setConnectionMetadata("Realtime RxJs example");
  8. // Establishes the connection to the Realtime server
  9. rxConnection.connect("YOUR_APP_KEY", "token");
  10. // Observe channel and subscribe to receive messages
  11. rxConnection.observeChannel("myChannel")
  12. .subscribe((message) => {
  13. console.log("Received message: " + message);
  14. });
  15. // Send a message
  16. 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).

API Reference

connect(appkey: string, token: string): void

Establishes a connection to a Realtime server using the given appkey and security token.

  1. rxConnection.connect("YOUR_APP_KEY", "token");

observeConnection(): Observable

Returns an Observable of connection events (e.g. onSubscribed, onReconneting, …).

  1. rxConnection.observeConnection()
  2. .subscribe((event) => {
  3. console.log("Realtime connection event:", event);
  4. });

observeChannel(channel: string): Observable

Returns an Observable for a Realtime channel. Each message received on the channel will emit a next event for each subscribed observer.

  1. rxConnection.observeChannel("myChannel")
  2. .subscribe((message) => {
  3. console.log("Received message: " + message);
  4. });

Note: the underlying Realtime channel unsubscription is performed automatically when there are no more observers subscribing the channel.

observeChannelWithOptions(channel: string, subscriberId?: string, filter?: string, autoUnsubscribe?: boolean): Observable

Same as observeChannel but uses other subscription options like buffered messages and filters.

  1. rxConnection.observeChannelWithOptions("myChannel", "mySubscriberId")
  2. .subscribe((m) => {
  3. console.log("Received message: " + m.message);
  4. console.log("Message id: " + m.seqId);
  5. });

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.

  1. rxConnection.send("myChannel", JSON.stringify({ foo: "bar" }));

publish(channel: string, message: string, ttl: number): Observable

Publishes a message to the given channel using the “at-least-once” delivery contract.

  1. rxConnection.publish("myChannel", JSON.stringify({ foo: "bar" }), 60)
  2. .subscribe((seqId) => {
  3. console.log("Message published with seqId: " + seqId);
  4. },
  5. (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.

  1. rxConnection.disconnect();

Other methods

This project wraps all public methods of the underlying Realtime JavaScript SDK. Complete reference can be found at
http://messaging-public.realtime.co/documentation/javascript/2.1.0/OrtcClient.html

Authors

Realtime.co