Telepat Javascript Client

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.

Installing

  • bower install telepat-js, if you're using Bower
  • npm install telepat-js, if you're using NPM
  • or the classic <script src="lib/telepat.js"></script>

Building from source

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.

License

Released under the Apache 2 License.

Telepat

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.

new Telepat()
Example
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
   });
 });
});
Instance Members
connected
connecting
configured
currentAppId
collections
subscriptions
admin
user
configure([options], [callback])
connect([options], [callback])
disconnect()
processMessage(message)
setLogLevel(level)
on(name, callback)
removeCallback(name, callbackId, index)
subscribe(options, onSubscribe)
getChannel(options)
Events
configure
connect
disconnect
login-error
error
logout-error
logout
collections-update
login

Channel

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.

new Channel()
Example
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();
});
Instance Members
objects
objectsArray
objectsCount
subscribe([callback])
unsubscribe([callback])
getCount([callback])
add(object, [callback])
remove(id, [callback])
update(id, patches, [callback])
on(name, callback)
removeCallback(name, callbackId, index)
Events
subscribe
unsubscribe
update
error

User

You can access an instance of this class using the user property of the Telepat object.

new User()
Example
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++;
 }
});
Instance Members
isAdmin
canReauth
data
reauth([callback])
update(id, patches, [callback])
requestPasswordReset(email, [callbackURL], [callback])
resetPassword(userId, id, token, newPassword, [callback])
register(user, [callback])
registerAdmin(admin, [callback])
loginWithFacebook(facebookToken, [callback])
login(email, password, [callback])
loginAdmin(email, password, [callback])
get(userId, [callback])
me([callback])
logout([callback])

Admin

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.

new Admin()
Example
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';
     }
   })
 }
});
Instance Members
userChannel
apps
app
users
getApps([callback])
addApp(properties, [callback])
updateApp(id, patches, [callback])
deleteApp(id, [callback])
addCollection(collection, [callback])
updateCollection(id, patches, [callback])
deleteCollection(id, [callback])
deleteModel(type, callback)
getAppUsers([callback])
addUser(user, [callback])
deleteUser(username, [callback])
updateUser(id, patches, [callback])
authorize(user, callback)
deauthorize(user, callback)

TelepatCallback

This callback is displayed as part of the Requester class.

TelepatCallback
Parameters
err (Error) If there was an error processing the requested operation, this will reference the error object resulted
res (any) The operation response