Samples for learning the Layer APIs.
There are three demos:
- The Client REST API
- The WebSocket API
- The Platform API
The examples require that you have npm
installed.
To get started, execute the following commands:
git clone git@github.com:layerhq/samples-web-apis.git
cd samples-web-apis
npm install
- Run your local webserver OR run
npm install http-server -g
followed bysudo http-server -a localhost -p 8080
.
Note: These demos can NOT be run from file:///
; only http://
.
To run the demos below, you should edit your config.js
file to have your LAYER_APP_ID
.
You should use the Application ID for the sample app generated for you in the Developer Dashboard; this is setup with an identity provider that the script will automatically access.
To run the Client REST API Demo, simply load rest-demo/index.html in your browser.
The ~/rest-library.js
file contains a library of functions for accessing the REST API.
The ~/rest-demo/demo.js
file a script of calls for invoking those rest-library.js's functions.
This demo is meant to be understood by running it with your debugger open and showing your network traffic.
To run the WebSocket API Demo, you need to be able to run the REST API Demo.
Simply load websocket-demo/demo.html
to run the demo; the demo will run the REST API Demo and show all WebSocket events generated. If you run this in chrome, you can see more details on the WebSocket events:
- Open the JavaScript debugger in chrome
- Go to the Network tab
- Select the "WebSocket" filter to filter the network requests listed
- Select the last WebSocket request shown in the network list
- Select the "frames" tab to see a list of all message frames sent through the WebSocket.
The Typing Indicator Demo comes with some libraries you are welcome to reuse, initializing three classes:
- TypingPublisher: Sends WebSocket packets to the server indicating that typing is "started", "paused" or "finished"
- TypingListener: Monitors a textbox you provide, and tells the TypingPublisher what the current state is
- TypingIndicatorListener: Monitors the WebSocket for typing indicators from other users, and fires onChange events whenever a typing indicator updates its state
These can be found in the typing-indicator-demo/typing_indicators
folder, and the demo.js shows an example of how to use them.
To run the Typing Indicator Demo, simply load typing-indicator.html
in your browser.
Open typing-indicator-demo/index.html
to run this test; you can test this in two browsers or tabs; one can log in as user "a" and the other as user "b" so you can see what both the sender and receiver of typing indicators will see.
The platform.js
script is a Node.js script that reads from your environment variables to get its configuration. Prior to running this sample you need to add the following environmental variables (unix example provided):
> export LAYER_APP_UUID=00000000-0000-0000-0000-000000000000
> export LAYER_PLATFORM_API_TOKEN=<token>
If you don't have a Platform API Bearer token, you can create one in the Developer Dashboard, in the Keys section.
Once everything is setup, you can run
> node platform.js
The platform.js
script provides some basic functions for accessing the platform api, and calls them in sequence.
The samples were lovingly crafted in San Francisco by the Layer team. At Layer, we are building the Communications Layer for the Internet. We value, support, and create works of Open Source engineering excellence.
These samples are available under the Apache 2 License. See the LICENSE file for more info.