RocketChat/asteroid

Name: asteroid

Owner: Rocket.Chat

Description: An alternative client for a Meteor backend

Created: 2016-01-20 01:55:15.0

Updated: 2016-09-03 07:05:15.0

Pushed: 2016-08-31 10:40:28.0

Homepage: http://mondora.github.io/asteroid

Size: 639

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Build Status Coverage Status

Example todo app using AngularJS. Same app using Meteor's front-end.

asteroid

A javascript client (browser and node) for a Meteor backend.

Table of contents

Why

Install

Example usage

Advantages over the canonical Meteor front-end

Build asteroid locally

Contribute

API

Why

Meteor is an awesome platform, but its canonical front-end is not very flexible. Asteroid gives the possibility to connect to a Meteor backend with any JS app.

Some of the things Asteroid allows you to do are:

Blog post on the library

Install
In the browser

First, dowload the library:

bower install asteroid

Then, add the necessary libraries to your index.html:

<script src="bower_components/ddp.js/src/ddp.js"></script>
<script src="bower_components/q/q.js"></script>
<script src="bower_components/asteroid/dist/asteroid.browser.js"></script>

If you want to login via oauth providers (facebook, google etc), also include the appropriate plugin:

<script src="bower_components/asteroid/dist/plugins/facebook-login.js"></script>

For facebook connect support in cordova via the facebook connect plugin, see https://github.com/keyvanfatehi/asteroid-facebook-connect

In a chrome extension or in cordova

Just replace asteroid.browser.js with asteroid.chrome.js or asteroid.cordova.js.

If using from within a chrome extension make sure to request for the tabs and storage permissions in your extensions manifest file.

In node

Download the package:

npm install asteroid

Require it in your project:

var Asteroid = require("asteroid");
Example usage
onnect to a Meteor backend
ceres = new Asteroid("localhost:3000");

se real-time collections
s.subscribe("tasksPublication");
tasks = ceres.getCollection("tasks");
s.insert({
scription: "Do the laundry"

et the task
laundryTaskRQ = tasks.reactiveQuery({description: "Do the laundry"});
og the array of results
ole.log(laundryTaskRQ.result);
isten for changes
dryTaskRQ.on("change", function () {
nsole.log(laundryTaskRQ.result);


ogin your user
s.loginWithTwitter();

all method and use promises via the Q library
ret = ceres.call('newUser');

result
hen(function (result) {
nsole.log('Success:', result);
atch(function (error) {
nsole.error('Error:', error);

Please refer to the Q documentation for more information about handling promises.

Advantages over the canonical Meteor front-end
Build asteroid locally

Clone the repository (or your fork) on your computer.

git clone https://github.com/mondora/asteroid

Enter the project's directory and install the required dependencies:

cd asteroid/
npm install

Start the development environment (requires gulp installed globally):

gulp

Visit localhost:8080/browser.html and localhost:8080/node.html for unit tests result.

Contribute

Contributions are as always very very welcome. If you want to help but don't know how to get started, feel free to schedule a pair programming session with me!

API
Asteroid methods
new Asteroid(host, ssl, interceptor)

Creates a new Asteroid instance, that is, a connection to a Meteor server (via DDP).

After being constructed, the instance will connect itself to the Meteor backend. It will also try, upon connection, to resume a previous login session (with a token saved in localstorage). The Asteroid.resumeLoginPromise property stores a promise which will be resolved if the resume was successful, rejected otherwise.

If SockJS is defined, it will be used as the socket transport. Otherwise WebSocket will be used. Note that SockJS is required for IE9 support.

Arguments Returns

An Asteroid instance.


Asteroid.on(event, handler)

Registers an event handler for the specified event.

Arguments

An Asteroid instance emits the following events:

Returns

Nothing


Asteroid.loginWith … ()

Logs the user in via the specified third party (oauth) service.

Available services Returns

A promise which will be resolved with the logged user id if the login is successful. Otherwise it'll be rejected with the error.


Asteroid.createUser(usernameOrEmail, password, profile)

Creates a user and logs him in. Does not hash the password before sending it to the server. This is not a problem, since you'll probably be using SSL anyway.

Arguments Returns

A promise which will be resolved with the logged user id if the creation and login are successful. Otherwise it'll be rejected with an error.


Asteroid.loginWithPassword(usernameOrEmail, password)

Logs the user in username/email and password. Does not hash the password before sending it to the server. This is not a problem, since you'll probably be using SSL anyway.

Arguments Returns

A promise which will be resolved with the logged user id if the login is successful. Otherwise it'll be rejected with an error.


Asteroid.logout()

Logs out the user.

Arguments

None

Returns

A promise which will be resolved with if the logout is successful. Otherwise it'll be rejected with the error.


Asteroid.subscribe(name, [param1, param2, …])

Subscribes to the specified subscription. If an identical subscription (same name and parameters) has already been made, Asteroid will return that subscription.

Arguments Returns

A subscription instance.


Asteroid.Subscription

Subscription instances have the following properties:

And the following method:


Asteroid.call(method, [param1, param2, …])

Calls a server-side method with the specified arguments.

Arguments Returns

An object with two properties: result and updated. Both properties are promises.

If the method is successful, the result promise will be resolved with the return value passed by the server. The updated promise will be resolved with nothing once the server emits the updated message, that tells the client that any side-effect that the method execution caused on the database has been reflected on the client (for example, if the method caused the insertion of an item into a collection, the client has been notified of said insertion).

If the method fails, the result promise will be rejected with the error returned by the server. The updated promise will be rejected as well (with nothing).


Asteroid.apply(method, params)

Same as Asteroid.call, but using as array of parameters instead of a list.

Arguments Returns

Same as Asteroid.call, see above.


Asteroid.getCollection(name)

Creates and returns a collection. If the collection already exists, nothing changes and the existing one is returned.

Arguments Returns

A reference to the collection.

Note

Asteroid auto-creates collections for you. For example, if you subscribe to an hypothetical posts subscription, the server will start sending the client added messages that refer to items of the posts collection. With Meteor's front-end we would normally need to define the postscollection before we can access it.

With Asteroid, when the first added message is received, if the posts collection doesn't exist yet, it will get automatically created. We can then get a reference to that collection by calling createCollection (or by accessing the semi-private Asteroid.collections dictionary).

Asteroid.Collection methods

All the following methods use latency compensation.

Collection.insert(item)

Inserts an item into a collection. If the item does not have an _id property, one will be automatically generated for it.

Arguments Returns

An object with two properties: local and remote. Both properties are promises.

The local promise is immediately resolved with the _id of the inserted item. That is, unless an error occurred. In that case, an exception will be raised. (TODO: this is a bit of an API inconsistency which maybe should be fixed).

The remote promise is resolved with the _id of the inserted item if the remote insert is successful. Otherwise it's rejected with the reason of the failure.


Collection.update(id, item)

Updates the specified item.

Arguments Returns

An object with two properties: local and remote. Both properties are promises.

The local promise is immediately resolved with the _id of the updated item. That is, unless an error occurred. In that case, an exception will be raised. (TODO: this is a bit of an API inconsistency which should be fixed).

The remote promise is resolved with the _id of the updated item if the remote update is successful. Otherwise it's rejected with the reason of the failure.

Note

The API greatly differs from Meteor's API. Aligning the two is on the TODO list.


Collection.remove(id)

Removes the specified item.

Arguments Returns

An object with two properties: local and remote. Both properties are promises.

The local promise is immediately resolved with the _id of the removed item. That is, unless an error occurred. In that case, an exception will be raised. (TODO: this is a bit of an API inconsistency which should be fixed).

The remote promise is resolved with the _id of the removed item if the remote remove is successful. Otherwise it's rejected with the reason of the failure.


Collection.reactiveQuery(selector)

Gets a “reactive” subset of the collection.

Arguments Returns

A ReactiveQuery instance.

ReactiveQuery methods and properties
ReactiveQuery.result

The array of items in the collection that matched the query.


ReactiveQuery.on(event, handler)

Registers a handler for an event.

Arguments

Possible events are:


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.