Web SDK 1.0
The Web SDK APIs are split into two main categories: Express and Low-Level. We recommend that you start with the Express API as this:
- Simplifies integration
- Automatically recovers from stream failures
- Handles edge cases such as network reconnects
Our Express APIs provide a simple, single-step API for the easiest integration of streaming into your application. Common use cases include:
- Channel (one to many) -> Channel
- Group Broadcast (few to many) -> Room
- Group Chat (many to many) -> Room
- One to One Chat -> Room
If you find that the Express API doesn't work for you, take a look at our Lower Level API.
Latest Release
Please refer to npmjs.org for the latest available version.
Bundling in your app
The Web SDK is available at npmjs.org and can be installed as a pre-built universal module using several dependency management tools. The Web SDK may be included directly in the browser or bundled using your favorite bundling tool such as Webpack or Rollup. For a full list of browser support see Browser Support.
Yarn
yarn add phenix-web-sdk
NPM
npm install -S phenix-web-sdk
Bower
bower install -S phenix-web-sdk
Bundles
The universal module is available uncompressed and minified.
phenix-web-sdk.js
phenix-web-sdk.min.js
Including with a bundler
Use your favorite bundling tool such as Webpack or Rollup.
ES6 Modules
import * as sdk from 'phenix-web-sdk';
// Or Import just what you need
import { express, utils, net } from 'phenix-web-sdk';
Common JS
const sdk = require('phenix-web-sdk');
AMD
define(['phenix-web-sdk'], function (sdk) {
// use sdk
});
Loading Directly in the Browser
Load it directly in the browser in a script tag.
var sdk = window['phenix-web-sdk'];
Initializing With Ember.js
If you are using ember.js for your frontend, then you can add the Web SDK by generating a vendor shim:
ember generate vendor-shim phenix-web-sdk
Then modify ember-cli-build.js as shown:
Add the following imports in your ember-cli-build.js:
app.import({
development: 'bower_components/phenix-web-sdk/dist/phenix-web-sdk.js',
production: 'bower_components/phenix-web-sdk/dist/phenix-web-sdk.min.js',
});
app.import('vendor/shims/phenix-web-sdk.js');
After that you can use the Web SDK in your code:
import sdk from 'phenix-web-sdk';
// use sdk
Production
We recommend that you include the minified version of the web-sdk in your production app or bundle(s). We optimize this bundle for size using smart transformations.
Alias with webpack
You can alias phenix-web-sdk in your webpack production config to use the minified version.
{
resolve: {
alias: {
'phenix-web-sdk': path.resolve(__dirname, '../node_modules', 'phenix-web-sdk/dist/phenix-web-sdk.min.js'),
}
}
}
Observable
Observe value changes. Similar to the RxJS Behavior Subject
[object Observable]
Name | Signature | Returns | Description |
---|---|---|---|
subscribe | (callback, options) | [object Subscription] | See Subscribe to an Observable |
getValue | () | any | See Get Observable Value |
setValue | (value) | void | See Set Observable Value |
Subscribe to an Observable
Receive a notification every time the observable value changes.
var subscription = observable.subscribe(function (newValue) {
// Do something with newValue
}, options);
setTimeout(function () {
// Stop listening to notifications after 1 second
subscription.dispose();
}, 1000);
Parameters
Name | Type | Description |
---|---|---|
callback (required) | Function | Function to call when value changes |
options (optional) | Object | Options for subscription. |
Options
Name | Type | Default Value | Description |
---|---|---|---|
initial (optional) | String | What to do when the subscription starts. Pass in 'notify' to be notified of initial value |
[object Subscription]
Name | Signature | Returns | Description |
---|---|---|---|
dispose | () | void | Stop listening to notifications |
Get Observable Value
Value may be of any supported javascript type
observable.getValue(value);
Set Observable Value
Change the observable value. This will trigger notifications on all subscribers.
observable.setValue(value);