UMD client for Telepat, built with Webpack to run in browsers. Available on GitHub.
Telepat is an open-source backend stack, designed to deliver information and information updates in real-time to clients, while allowing for flexible deployment and simple scaling. Read more about how it works and why it's different here.
Documentation is available here, and you can check out a simple demo app here.
bower install telepat-js
, if you're using Bowernpm install telepat-js
, if you're using NPM<script src="lib/telepat.js"></script>
Clone the repo, then run npm install
. After editing the sources in the /src directory, run npm run build-all
to compile the libraries, and npm run docs
to generate the documentation.
Released under the Apache 2 License.
The Telepat
object is the first object you want to instantiate while working with the Telepat SDK.
It exposes methods and properties that enable you to register, login, subscribe to objects and to users.
let telepat = new Telepat();
telepat.connect({
apiEndpoint: 'TELEPAT-API-ENDPOINT',
socketEndpoint: 'TELEPAT-SOCKET-ENDPOINT',
apiKey: 'APP-API-KEY',
appId: 'APP-ID'
}, (err, res) => {
if (err) {
// Treat connection error
console.log(err);
return;
}
// Display all collections
console.log(telepat.collections);
// Login, display and update user data
telepat.on('login', () => {
console.log(telepat.user.data);
telepat.user.data.change = true;
});
telepat.user.login('user', 'pass');
// Subscribe to data
let articleChannel = telepat.subscribe({
channel: {
context: 'collection-identifier',
model: 'article'
}
}, () => {
console.log(articleChannel.objectsArray);
articleChannel.objects['object-identifier'].title = 'new title';
articleChannel.on('update', (operationType, objectId, object, oldObject) => {
// Update interface on data updates
});
});
});
Indicates whether the current instance is connected to the backend
Indicates whether the current instance is in the process of connecting to the backend.
If true, the connect
event will be fired as soon as connection is established.
Indicates whether the current instance is properly configured and ready for connection.
If connected, this property reflects the current app id.
This object contains details about all the collections available for the currently connected application.
You can read this after the connect
event is emitted, or if the connected
property is true.
Each available collection is stored as an Object, using a key whose name is equal to the collection's id.
Modifications to collection objects stored within will be automatically synchronized with the Telepat backend.
This object contains references to all of the Channels that are actively subscribed. Each channel is stored using a key equal to the channel's unique identifier.
This property becomes available after successfully logging in as an administrator. It gives you access to a instance of the Admin class, allowing you access to administrator functionality.
telepat.user.loginAdmin('admin@email.com', 'password', (err) => {
if (err) {
// Treat login error
} else {
telepat.admin.getAppUsers((err) => {
if (err) {
// Treat error
} else {
// Treat success
console.log(telepat.admin.users);
}
})
}
});
An instance of the User class, this allows you to access user functionality like logging in, accessing and modifying current user data or registering new user accounts.
telepat.user.login('email', 'password', (err) => {
if (err) {
// Treat login error
} else {
// Treat successful login
console.log(telepat.user.data);
}
});
Call this to configure Telepat server endpoints without connecting to a specific app.
([Object](default {}))
Object containing all configuration options for connection
Name | Description |
---|---|
options.apiEndpoint string
|
The Telepat API endpoint URL |
options.socketEndpoint string
|
The Telepat socket endpoint URL |
options.reauth [boolean]
(default false )
|
Should reauth previously logged in user on connection |
([TelepatCallback](default () => {}))
Callback invoked after configuration is finished
let telepat = new Telepat();
telepat.configure({
apiEndpoint: 'TELEPAT-API-ENDPOINT',
socketEndpoint: 'TELEPAT-SOCKET-ENDPOINT'
}, (err, res) => {
// Handle configuration
});
Call this to connect to a specific Telepat app. This is usually the first thing you need to do after instantiating the Telepat object.
([Object](default {}))
Object containing all configuration options for connection
Name | Description |
---|---|
options.appId string
|
Your app id |
options.apiEndpoint [string]
|
The Telepat API endpoint URL. If this is absent from the connect options, it must have been previously set by calling configure . |
options.socketEndpoint [string]
|
The Telepat socket endpoint URL. If this is absent from the connect options, it must have been previously set by calling configure . |
options.apiKey string
|
Your app API key |
options.persistentConnection [Object]
(default null )
|
Set this to configure receiving updates via persistent channels, like push notifications. |
options.ioOptions [Object]
(default {} )
|
Configuration options for socket.io |
options.updateUDID [boolean]
(default false )
|
Set this to true to force the client to update the saved device identifier. |
options.timerInterval [number]
(default 150 )
|
Frequency of running diff (in miliseconds) to check for object updates. |
options.reauth [boolean]
(default false )
|
Should reauth previously logged in user on connection |
([TelepatCallback](default () => {}))
Callback invoked after configuration is finished
// Simple connection to backend
let telepat = new Telepat();
telepat.connect({
apiEndpoint: 'TELEPAT-API-ENDPOINT',
socketEndpoint: 'TELEPAT-SOCKET-ENDPOINT',
apiKey: 'APP-API-KEY',
appId: 'APP-ID'
}, (err, res) => {
// Handle connection
});
// Using connection event
let telepat = new Telepat();
telepat.connect({
apiEndpoint: 'TELEPAT-API-ENDPOINT',
socketEndpoint: 'TELEPAT-SOCKET-ENDPOINT',
apiKey: 'APP-API-KEY',
appId: 'APP-ID'
});
let connectCallbackId = telepat.on('connect', {
telepat.removeCallback(connectCallbackId);
// Handle connection
});
// Activating the push notifications transport.
// Do this when running inside a mobile OS, for example.
telepat.connect({
apiEndpoint: 'TELEPAT-API-ENDPOINT',
socketEndpoint: 'TELEPAT-SOCKET-ENDPOINT',
apiKey: 'APP-API-KEY',
appId: 'APP-ID',
persistentConnection: {
type: 'ios',
token: 'DEVICE-NOTIFICATION-TOKEN',
active: 1
}
});
Call this function to disconnect the client from the Telepat backend.
Forwards messages reveived via external channels to the processing unit. Use this if you've configured external transports (like push notifications), and you need to pass received payloads to the processing engine.
(string)
The delta update notification received from Telepat
Call this function to add callbacks to be invoked on event triggers. Available callbacks:
Name | Description |
---|---|
connect | Invoked when client has connected to the backend |
disconnect | Invoked when client has disconnected from the backend |
configure | Invoked when client configuration has completed |
error | Invoked on any operation error |
collections-update | Invoked when the available collections have updated |
login | Invoked when client has successfully logged in |
login-error | Invoked when there was an error with logging in |
logout | Invoked when client has successfully logged out |
logout-error | Invoked when there was an error with logging out |
(string)
The name of the event to associate the callback with
(function)
The callback to be executed
number
:
A callback id. Save this in order to later remove the callback from the event (using
removeCallback
)
telepat.on('connect', () => {
console.log('connected');
});
Call this function to remove callbacks that have been set using on.
(string)
The name of the event the callback was associated with
(any)
let connectCallbackId = telepat.on('connect', () => {
// Remove the callback after the first connection event
telepat.removeCallback(connectCallbackId);
});
Use this function to create a new Channel object and retrieve its objects.
(Object)
The object describing the required subscription
Name | Description |
---|---|
options.limit [number]
|
The maximum number of objects to be returned in this batch (for pagination) |
options.channel [Object]
|
Describes the basic properties of the objects requested |
options.sort [Object]
|
An object that defines how returned objects should be sorted. Each object key is a property name, and each value can be either
asc
or
desc
.
|
options.filters [Object]
|
An object describing how returned objects should be filtered. |
options.offset [number]
|
The offset that should be applied for the returned objects (for pagination) |
(function)
Callback invoked when subscription is ready
Channel
:
The new
Channel
object
// A simple subscription to all objects of type `article`
// in a specific collection
let articleChannel = telepat.subscribe({
channel: {
context: 'context-unique-identifier',
model: 'article'
}
}, () => {
console.log(articleChannel.objectsArray);
});
// A filtered subscription to all objects of type `article`
// in a specific collection, that have one of two specific tag values
let articleChannel = telepat.subscribe({
channel: {
context: 'context-unique-identifier',
model: 'article'
},
filters: {
or: [
{
is: {
tag: 'specific-tag-value'
}
},
{
is: {
tag: 'another-tag-value'
}
}
]
}
}, () => {
console.log(articleChannel.objectsArray);
});
// A simple subscription to all objects of type `article`
// in a specific collection, sorted by created date descending
let articleChannel = telepat.subscribe({
channel: {
context: 'context-unique-identifier',
model: 'article'
},
sort: {
created: 'desc'
}
}, () => {
console.log(articleChannel.objectsArray);
});
// A simple subscription to all objects of type `comment`
// in a specific collection, that belong to a specific article parent
let articleChannel = telepat.subscribe({
channel: {
context: 'context-unique-identifier',
model: 'comment',
parent: {
model: 'article',
id: 'article-parent-unique-identifier'
}
}
}, () => {
console.log(articleChannel.objectsArray);
});
Invoked when client configuration has completed.
(any)
(any)
Invoked when client has connected to the backend.
(any)
(any)
Invoked when client has disconnected from the backend.
(any)
(any)
Invoked when there was an error with logging in.
(any)
(any)
Invoked on any operation error.
(any)
(any)
Invoked when there was an error with logging out.
(any)
(any)
Invoked when client has successfully logged out.
(any)
(any)
Invoked when the available collections have updated.
(any)
(any)
Invoked when client has successfully logged in.
(any)
(any)
Use Channels to create, update and remove Telepat objects. You can create new Channels using the subscribe or the getChannel methods of the main Telepat object.
let articleChannel = telepat.subscribe({
channel: {
context: 'context-unique-identifier',
model: 'article'
}
}, () => {
console.log(articleChannel.objectsArray);
// Create a new article object
articleChannel.objects['new'] = {
title: 'New article',
text: 'Article body'
};
// Update a specific article
articleChannel.objects['article-unique-id'].title = 'New title';
// Delete a specific article
delete articleChannel.objects['article-unique-id'];
// React to object updates
articleChannel.on('update', (operationType, objectId, object, oldObject) => {
console.log(`Received article update of type ${operationType}, for object with id ${objectId}`);
// Objects are already updated
console.log(articleChannel.objects);
});
// Unsubscribe and clear objects
articleChannel.unsubscribe();
});
A container object referencing all of the objects retrieved via subscription. Each object is stored on a key equal to its own unique id.
A container array referencing all of the objects retrieved via subscription. The order of the objects reflects the sorting options set for the channel.
The current object count.
Call this function to perform the actual subscribe for the configured channel. This is usually invoked by the subscribe method on the main Telepat object.
([TelepatCallback](default () => {}))
Callback invoked after subscribe is finished
Call this function to unsubscribe from the configured channel. All channel properties will be reset to original values.
([TelepatCallback](default () => {}))
Callback invoked after unsubscribe is finished
Call this to retrieve the number of objects available for this channel. Value will be available on the objectsCount property.
([TelepatCallback](default () => {}))
Callback invoked after getting object count is finished
Add a new Telepat object to the current channel. Instead of using this function, you can also add the object to Channel.objects, on any new key. The key will be automatically replaced with the new object id after the backend processes the operation.
(Object)
The new object to add
([TelepatCallback](default () => {}))
Callback invoked after notifying the Telepat backend of the new object
// This is one way of adding a new object using the channel instance.
// The new key will be picked up by the monitoring system, which will signal the new event creation
// to the Telepat backend, and then delete the new key you just set from the objects property.
// After processing the request, Telepat will signal the change back to the client, and the new object will be
// re-added to the objects property, but this time on the right key (equal to the new object's assigned id).
// This is when the 'update' event will get triggered on the channel.
channel.objects['new'] = newObject;
// Alternatively, you can call the add method:
channel.add(newObject, err => {
if (err) {
// There's been a server error, check err. The object will not be added.
} else {
// We've successfully signaled that we want to add the object.
// Keep in mind that this is not a sync process, so we still have to wait for the 'update'
// event on the channel, signaling the availability of the new object on the channel.objects property.
}
});
Remove a Telepat object from the current channel. Instead of using this function, you can also delete the object from Channel.objects.
(string)
The id of the object to delete
([TelepatCallback](default () => {}))
Callback invoked after notifying the Telepat backend of the deleted object
// This is one way of deleting an object using the channel instance.
// The deleted key will be picked up by the monitoring system, which will signal the object removal
// to the Telepat backend. The change will be then signaled back to the client as confirmation, triggering
// the channel's 'update' event.
// This is the optimistic way of doing a delete, as the removed object will be instantly gone from the local
// state, before the 'update' event is triggered (and even if it is not).
delete channel.objects[objectId];
// Alternatively, the pessimistic approach is to call the remove method:
channel.remove(objectId, err => {
if (err) {
// There's been a server error, check err. The object will not be deleted.
} else {
// We've successfully signaled that we want to delete the object.
// Keep in mind that this is not a sync process, so we still have to wait for the 'update'
// event on the channel, signaling that the object has been removed from the channel.objects property.
}
});
Updates a Telepat object from the current channel. To call this function, you need to create an array containing 'patch' objects, representing the modifications that need to be persisted. The structure of a patch object is:
{'op': 'replace', 'path': channel + '/' + object_id + '/' + object_property, 'value': modified_value}
Instead of using this function, you can also update the object directly from Channel.objects.
(number)
The id of the object to update
([TelepatCallback](default () => {}))
Callback invoked after notifying the Telepat backend of the updated object
// This is one way of updating an object using the channel instance.
// The updated key will be picked up by the monitoring system, which will signal the object update
// to the Telepat backend. The change will be then signaled back to the client as confirmation, triggering
// the channel's 'update' event.
// This is the optimistic way of doing a delete, as the updated object will be instantly modified within the local
// state, before the 'update' event is triggered (and even if it is not).
channel.objects[objectId].title = "New title";
// Alternatively, the pessimistic approach is to call the update method:
channel.update(objectId, [
{
'op': 'replace',
'path': `article/${objectId}/title`,
'value': 'New title'
}
], err => {
if (err) {
// There's been a server error, check err. The object will not be updated.
} else {
// We've successfully signaled that we want to update the object.
// Keep in mind that this is not a sync process, so we still have to wait for the 'update'
// event on the channel, signaling that the object has been updated within the channel.objects property.
}
});
Call this function to add callbacks to be invoked on event triggers. Available callbacks:
Name | Description |
---|---|
error | Invoked when there was an error processing the requested operation |
subscribe | Invoked when channel subscription is successful |
unsubscribe | Invoked when channel unsubscription is successful |
update | Invoked when objects in the subscription have been modified (update of an existing object, new object or deleted object) |
(string)
The name of the event to associate the callback with
(function)
The callback to be executed
number
:
A callback id. Save this in order to later remove the callback from the event (using
removeCallback
)
// React to object updates
articleChannel.on('update', (operationType, objectId, object, oldObject) => {
console.log(`Received article update of type ${operationType}, for object with id ${objectId}`);
// Objects are already updated
console.log(articleChannel.objects);
});
Call this function to remove callbacks that have been set using on.
(string)
The name of the event the callback was associated with
(any)
let updateCallbackId = channel.on('update', () => {
// Remove the callback after the first update event
channel.removeCallback(updateCallbackId);
});
Invoked when channel subscription is successful.
(any)
(any)
Invoked when channel unsubscription is successful.
(any)
(any)
Invoked when objects in the subscription have been modified (there was an update of an existing object, a new object has been added or an object has been deleted).
Invoked when there was an error processing the requested operation.
(any)
(any)
You can access an instance of this class using the user property of the Telepat object.
telepat.user.login('email', 'password', (err) => {
if (err) {
// Treat login error
} else {
// Treat successful login
console.log(telepat.user.data);
// Update user data
telepat.user.data.points++;
}
});
Indicates if the currently logged in user is an admin
Indicates if there's a saved authentication token that can be used to re-login
Object that holds all key-value data about the currently logged in user
If there is a saved authentication token from previous connections, try to use it to login again. You can call this method if the canReauth property is true.
([TelepatCallback](default () => {}))
Callback invoked after reauth operation is finished
Call this to update your profile.
To call this method, you need to create an array containing 'patch' objects, representing the modifications that need to be persisted. The structure of a patch object is:
{'op': 'replace', 'path': user or admin + '/' + user_id + '/' + property, 'value': modified_value}
Instead of using this function, you can also update the user directly from User.data.
(string)
The user id of the updated user profile
([TelepatCallback](default () => {}))
Callback invoked after operation is finished
Call this to request a password reset for the logged in user. The process involves a confirmation email, with a link that needs to be clicked on in order to get a unique pass reset token. You then use that token to call the resetPassword method that finishes the process by setting a new password.
(string)
The email/username of the user to reset the pass for
([string](default null))
The URL the user will be pointed to after verifying the request by clicking the link in the sent email
([TelepatCallback](default () => {}))
Callback invoked after the operation is finished
(string)
The id of the user that needs the password reset
(any)
(string)
The new password
([TelepatCallback](default () => {}))
Callback invoked after the operation is finished
This function creates a new user profile.
(Object)
The object representing the new user profile
([TelepatCallback](default () => {}))
Callback invoked after the operation is finished
This function creates a new admin profile.
(Object)
The object representing the new admin profile
([TelepatCallback](default () => {}))
Callback invoked after the operation is finished
This function associates the current anonymous device to a Telepat user profile, using a Facebook account for authentication.
(string)
The user token obtained from Facebook after login
([TelepatCallback](default () => {}))
Callback invoked after the operation is finished
This function associates the current anonymous device to a Telepat user profile, using a password for authentication.
(string)
The user's email address
(string)
The user's password
([TelepatCallback](default () => {}))
Callback invoked after the operation is finished
telepat.user.login('email', 'password', (err) => {
if (err) {
// Treat login error
} else {
// Treat successful login
console.log(telepat.user.data);
}
});
This function associates the current anonymous device to a Telepat administrator profile, using a password for authentication.
(string)
The admin email address
(string)
The admin password
([TelepatCallback](default () => {}))
Callback invoked after the operation is finished
telepat.user.loginAdmin('admin@email.com', 'password', (err) => {
if (err) {
// Treat login error
} else {
telepat.admin.getAppUsers((err) => {
if (err) {
// Treat error
} else {
// Treat success
console.log(telepat.admin.users);
}
})
}
});
Call this to retrieve a specific application user object. Results will be sent as a callback argument.
(string)
The id of the requested user
([TelepatCallback](default () => {}))
Callback invoked after operation is finished
This function retrieves the currently logged in user's information.
([TelepatCallback](default () => {}))
Callback invoked after the operation is finished
Logs the current user out.
([TelepatCallback](default () => {}))
Callback invoked after the operation is finished
You can access an instance of this class using the admin property of the Telepat object. This instance becomes available after successfully logging in as an administrator.
telepat.user.loginAdmin('admin@email.com', 'password', (err) => {
if (err) {
// Treat login error
} else {
telepat.admin.getAppUsers((err) => {
if (err) {
// Treat error
} else {
// Treat success
console.log(telepat.admin.users);
// Update users
telepat.admin.users[goodUserId].isAwesome = true;
delete telepat.admin.users[badUserId];
// Update collection metadata
telepat.collections[currentCollectionId].topic = 'Cats';
}
})
}
});
This Channel instance allows registering callbacks for 'update' events on user data.
This object contains data about all of the applications the current administrator can access. Each app data is stored using a key equal to the application unique identifier. You can access this after calling getApps. Modifications to app objects stored within will be automatically synchronized with the Telepat backend.
This object contains data about the currently connected app. You can access this after calling getApps. Modifications to this object will be automatically synchronized with the Telepat backend.
This object contains data about all of the users of the current app. Each user data is stored using a key equal to the user unique identifier. You can access this after calling getAppUsers. Modifications to user objects stored within will be automatically synchronized with the Telepat backend.
Call this to retrieve all the application objects the current administrator can access. Results will be sent as a callback argument, and persisted on the apps property. The object belonging to the currently connected app will also be made available, on the app property.
([TelepatCallback](default function () {}))
Callback invoked after operation is finished
Call this to create a new application.
(Object)
Data about the new application. Can hold any key-value data.
Must contain at least a 'keys' key, with an array of string values that will be used as valid API keys for the app.
([TelepatCallback](default function () {}))
Callback invoked after operation is finished
Updates key-value data within an application object.
To call this method, you need to create an array containing 'patch' objects, representing the modifications that need to be persisted. The structure of a patch object is:
{'op': 'replace', 'path': application + '/' + app_id + '/' + property, 'value': modified_value}
Instead of using this function, you can also update the app directly from Admin.apps.
(string)
The application id
([TelepatCallback](default () => {}))
Callback invoked after operation is finished
Call this to delete an application. Instead of using this function, you can also delete the app directly from Admin.apps.
(string)
The application id
([TelepatCallback](default function () {}))
Callback invoked after operation is finished
Call this to create a new collection.
(Object)
Data about the new application. Can hold any key-value data. May be empty object.
([TelepatCallback](default () => {}))
Callback invoked after operation is finished
Updates key-value data within a collection object.
To call this method, you need to create an array containing 'patch' objects, representing the modifications that need to be persisted. The structure of a patch object is:
{'op': 'replace', 'path': context + '/' + collection_id + '/' + property, 'value': modified_value}
Instead of using this function, you can also update the collection directly from Telepat.collections.
(string)
The collection id
([TelepatCallback](default () => {}))
Callback invoked after operation is finished
Call this to delete a collection. Instead of using this function, you can also delete the collection directly from Telepat.collections.
(string)
The collection id
([TelepatCallback](default () => {}))
Callback invoked after operation is finished
Call this to remove a model from the schema, together with all objects of that specific type.
(string)
The model name
(TelepatCallback)
Callback invoked after operation is finished
Call this to retrieve all the current application user objects. Results will be sent as a callback argument, and persisted on the users property.
([TelepatCallback](default function () {}))
Callback invoked after operation is finished
This is an alias for #User#register.
(Object)
The object representing the new user profile
([TelepatCallback](default function () {}))
Callback invoked after operation is finished
Call this to delete a user profile. Instead of using this function, you can also delete the user directly from Admin.users.
(string)
The email address of the user profile to delete
([TelepatCallback](default function () {}))
Callback invoked after operation is finished
Call this to update a user profile.
To call this method, you need to create an array containing 'patch' objects, representing the modifications that need to be persisted. The structure of a patch object is:
{'op': 'replace', 'path': user + '/' + user_id + '/' + property, 'value': modified_value}
Instead of using this function, you can also update the user directly from Admin.users.
(string)
The id of the user to be updated
([TelepatCallback](default function () {}))
Callback invoked after operation is finished
Call this to authorize access to the current app for another administrator account within the same Telepat instance.
(string)
The email associated with the account of the new administrator
(TelepatCallback)
Callback invoked after operation is finished
Call this to deauthorize access to the current app for another administrator account within the same Telepat instance.
(string)
The email associated with the account of the administrator to be removed
(TelepatCallback)
Callback invoked after operation is finished
This callback is displayed as part of the Requester class.
(Error)
If there was an error processing the requested operation, this will reference the error object resulted
(any)
The operation response