Constructor
new Webcom(app, optionsopt, appOrRepo, pathOrContextOrOptions)
Webcom
instance.Webcom
instance. Only the first argument is exposed for
public use and must be a api.WebcomApp
instance.
Parameters:
Name | Type | Attributes | Default | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
app |
api.WebcomApp |
|
|||||||||||||||||
options |
Object |
<optional> |
{} | Properties
|
|||||||||||||||
appOrRepo |
String |
|
|||||||||||||||||
pathOrContextOrOptions |
String |
Example
// Get a reference to the "contacts" application
var ref = new Webcom('contacts');
Extends
Members
App :api.WebcomApp
Convenient link to the api.WebcomApp
class.
Type:
- Version:
- 2.8+
authState :Object
Returns the current authentication state based on the DataPulse Authentication Service model.
Type:
- Object
- Version:
- 2.8+
Methods
addAccount(provider, details, messageopt) → {Promise}
Creates a new identity managed by the DataPulse Authentication Service.
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
provider |
string | The internal provider to use to create the identity. Currently only |
|||||||||||||||||||||
details |
object | The details associated with the identity to create. Properties
|
|||||||||||||||||||||
message |
object |
<optional> |
The parameters to apply to the verification message to be sent to the user. Properties
|
- Version:
- 2.6.2+
- Deprecated:
- use
Webcom#addIdentity
instead
- use
Returns:
A promise that will be resolved once the identity creation successfully completed. In case of
success (reps. failure), the resolve (resp. reject) callback is called with one
argument filled with the same value than the auth
(resp. error
) parameter of
authentication callbacks.
Note that the just created identity is in the "unverified" state and cannot be used to sign a
user in until it is verified. To do so, the user will receive a token or a code on the
specified email address or MSISDN, which must be passed back to the
Webcom#verifyIdentity
method.
- Type
- Promise
addAuthCallback(callback) → {Webcom.authCallback}
Adds an authentication callback function to the set of functions subscribed to changes of the authentication state. This function will be (asynchronously) called with the current authentication state, and then each time the authentication state changes.
Parameters:
Name | Type | Description |
---|---|---|
callback |
Webcom.authCallback | the callback function to add. |
- Version:
- 2.8+
- See:
Returns:
the added callback function. Note that it is useful to keep a reference on this
function as soon as you need to remove it from the subscribed functions using the
Webcom#removeAuthCallback
method.
- Type
- Webcom.authCallback
addIdentity(provider, details, messageopt) → {Promise}
Creates a new identity managed by the DataPulse Authentication Service.
If the Webcom#makeNextAuthOperationContextual
method is called just before this method, then the new
identity is bound to the DataPulse account of the currently authenticated user. Otherwise (or if no user is
currently authenticated), the new identity is bound to a new DataPulse account (to be created at the same
time).
Currently, DataPulse is able to manage email- and phone-based identities.
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
provider |
string | The internal provider to use to create the identity. Currently only |
|||||||||||||||||||||
details |
object | The details associated with the identity to create. Properties
|
|||||||||||||||||||||
message |
object |
<optional> |
The parameters to apply to the verification message to be sent to the user. Properties
|
- Version:
- 2.8+
- See:
Returns:
A promise that will be resolved once the identity creation successfully completed. In case of
success (reps. failure), the resolve (resp. reject) callback is called with one
argument filled with the same value than the auth
(resp. error
) parameter of
authentication callbacks.
Note that the just created identity is in the "unverified" state and cannot be used to sign a
user in until it is verified. To do so, the user will receive a token or a code on the
specified email address or MSISDN, which must be passed back to the
Webcom#verifyIdentity
method.
- Type
- Promise
auth(cred, onCompleteopt)
Authenticates the user using a (already available) Webcom authentication token.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
cred |
String | The Webcom authentication token to use |
|
onComplete |
Webcom.authCallback |
<optional> |
Callback function to be called when the authentication completes. |
- Deprecated:
- use
authWithToken()
instead.
- use
authAnonymously(onCompleteopt) → {Promise}
Authenticates the user using a temporary account (to be lost at next sign out or at authentication token expiration) with no explicit credentials.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
onComplete |
Webcom.authCallback |
<optional> |
Callback function to be called when the authentication completes. |
- Version:
- 1.1+
- See:
Returns:
The authentication is performed asynchronously, and its result may be caught by assigning a
resolve and/or a reject callback to the returned Promise
. The resolve (resp.
reject) callback is called with one argument filled with the same value than the auth
(resp. error
) parameter of authentication callbacks.
- Type
- Promise
Example
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// Anonymously log the user in
ref.authAnonymously().then(function(auth) {
console.log("User logged in, with UID", auth.uid);
});
// ==> ends up in calling authentication callbacks previously registered using resume()
authInternally(provider, credentials) → {Promise}
Authenticates the user using a given DataPulse internally managed authentication method among:
- Email-based authentication, with a personal password,
- Phone-base authentication, with a one time password (OTP). In this case, an OTP must be sent to the user
beforehand using the
Webcom#sendOtp
method and the returned challenge identifier must be kept (see thecredentials.challenge
parameter).
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
provider |
string | The authentication method to use: |
||||||||||||
credentials |
object | The user's credentials. Their content depends on the chosen authentication method. Properties
|
- Version:
- 2.8+
- See:
Returns:
The authentication is performed asynchronously, and its result may be caught by assigning a
resolve and/or a reject callback to the returned Promise
. The resolve (resp.
reject) callback is called with one argument filled with the same value than the auth
(resp. error
) parameter of authentication callbacks.
- Type
- Promise
authWithCustomProvider(provider, credentials, onCompleteopt) → {Promise}
Authenticates the user using credentials provided by a third-party ad-hoc identity provider.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
provider |
String | Identifier of the custom provider to use. |
|
credentials |
String | Credentials for the user to authenticate, to be supplied by the 3rd-party identity provider. |
|
onComplete |
Webcom.authCallback |
<optional> |
Callback function to be called when the authentication completes. |
- Version:
- 2.2.2+
- See:
Returns:
The authentication is performed asynchronously, and its result may be caught by assigning a
resolve and/or a reject callback to the returned Promise
. The resolve (resp.
reject) callback is called with one argument filled with the same value than the auth
(resp. error
) parameter of authentication callbacks.
- Type
- Promise
Example
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// Log the user in, using a 3rd-party identity provider
ref.authWithCustomProvider("myprovider", "theprovidedcredentialsfortheusertoauthenticate").then(function(auth) {
console.log("User logged in, with UID", auth.uid);
});
// ==> ends up in calling authentication callbacks previously registered using resume()
authWithOAuth(provider, optionsopt, onCompleteopt) → {Promise}
Authenticates the user using a 3rd-party OAuth2 provider.
Parameters:
Name | Type | Attributes | Description | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
provider |
string | The OAuth2 provider to use. Currently supported providers are:
|
||||||||||||||||||||||||||
options |
object |
<optional> |
The authentication options to use, with the following properties: Properties
|
|||||||||||||||||||||||||
onComplete |
Webcom.authCallback |
<optional> |
Callback function to be called when the authentication completes. |
- Version:
- 1.3+
- See:
Returns:
The authentication is performed asynchronously, and its result may be caught by assigning a
resolve and/or a reject callback to the returned Promise
. The resolve (resp.
reject) callback is called with one argument filled with the same value than the auth
(resp. error
) parameter of authentication callbacks.
- Type
- Promise
Examples
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// Log the user in with Orange France using a popup
ref.authWithOAuth('orange');
// ==> ends up in calling authentication callbacks previously registered using resume()
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// Log the user in with Facebook using redirection
ref.authWithOAuth('facebook', {mode: 'redirect'});
// ==> ends up in reloading the page
// the authentication callbacks will be then called as soon as registered using resume()
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// Log the user in with Google using an authorization code (returned by the Google SDK)
ref.authWithOAuth('google', {mode: 'code', code:'XXX...XXX'});
// ==> ends up in calling authentication callbacks previously registered using resume()
authWithPassword(credentials, onCompleteopt) → {Promise}
Authenticates the user using her⋅his email and personal password.
Parameters:
Name | Type | Attributes | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
credentials |
Object | The user's credentials. Properties
|
||||||||||
onComplete |
Webcom.authCallback |
<optional> |
Callback function to be called when the authentication completes. |
- Deprecated:
- use
Webcom#authInternally
instead
- use
Returns:
The authentication is performed asynchronously, and its result may be caught by assigning a
resolve and/or a reject callback to the returned Promise
. The resolve (resp.
reject) callback is called with one argument filled with the same value than the auth
(resp. error
) parameter of authentication callbacks.
- Type
- Promise
authWithPhone(credentials) → {Promise}
Authenticates the user using her⋅his MSISDN and a one time password
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
credentials |
object | The user's credentials. Properties
|
- Version:
- 2.6.2+
- Deprecated:
- use
Webcom#authInternally
instead
- use
Returns:
The authentication is performed asynchronously, and its result may be caught by assigning a
resolve and/or a reject callback to the returned Promise
. The resolve (resp.
reject) callback is called with one argument filled with the same value than the auth
(resp. error
) parameter of authentication callbacks.
- Type
- Promise
authWithToken(token, onCompleteopt) → {Promise}
Authenticates the user using a (already available) Webcom authentication token. This method is mainly intended for Node.js back end.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
token |
String | The Webcom authentication token to use |
|
onComplete |
Webcom.authCallback |
<optional> |
Callback function to be called when the authentication completes. |
- Version:
- 1.3+
Returns:
The authentication is performed asynchronously, and its result may be caught by assigning a
resolve and/or a reject callback to the returned Promise
. The resolve (resp.
reject) callback is called with one argument filled with the same value than the auth
(resp. error
) parameter of authentication callbacks.
- Type
- Promise
Example
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// Log the user in with a custom-made Webcom authentication token
ref.authWithToken('<THE-TOKEN>').then(function(auth) {
console.log("User logged in, with UID", auth.uid);
});
// ==> ends up in calling authentication callbacks previously registered using resume()
changePassword(email, oldPassword, newPassword, onCompleteopt) → {Promise}
Changes the password of an existing email-based identity.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
email |
String | The email of the identity, the password of which has to be updated |
|
oldPassword |
String | The current password of the identity |
|
newPassword |
String | The new password to set |
|
onComplete |
function |
<optional> |
A 1-argument callback function that is called after the password change
completion, which receives an |
Returns:
- Type
- Promise
Example
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// Change the password associated with "john@webcom.com" identity
ref.changePassword("john@webcom.com","pwd","newPwd", function(error) {
if (error) {
switch (error.code) {
case "INVALID_CREDENTIALS":
console.log("The email doesn't exist or the current password is incorrect!");
break;
case "TOO_WEAK_PASSWORD":
console.log("The new password is not strong enough!");
break;
default:
console.log("The password change failed: " + error.message);
}
} else {
console.log("Password changed successfully!");
}
});
child(pathString) → {Webcom}
Gets a Webcom
instance that refers to a specific data node within the application data tree, specified as
a relative path to the data node referred to by this Webcom
instance.
Parameters:
Name | Type | Description |
---|---|---|
pathString |
String | Data path, relative to the location of this
|
Returns:
A Webcom
instance that refers to the specified path
- Type
- Webcom
Example
// Get a reference to the "friends" data node
var ref = new Webcom("contacts").child("friends");
var johnRef = ref.child("john");
console.log(johnRef.name());
// ==> "/https/io.datasync.orange.com/contacts//friends/john"
var maryRef = ref.child("/mary");
console.log(maryRef.toString());
// ==> "/https/io.datasync.orange.com/contacts//friends/mary"
var maryRef2 = johnRef.child("../mary");
console.log(maryRef2.toString());
// ==> "/https/io.datasync.orange.com/contacts//friends/mary"
var nameThenLastRef = johnRef.child("name").child("last");
console.log(nameThenLastRef.toString());
// ==> "/https/io.datasync.orange.com/contacts//friends/john/name/last"
var lastNameRef = johnRef.child("name/last");
console.log(lastNameRef.toString());
// ==> "/https/io.datasync.orange.com/contacts//friends/john/name/last"
var rootRef = ref.child("../..");
console.log(rootRef.toString());
// ==> "/https/io.datasync.orange.com/contacts/"
var hashedRef = ref.child("#john");
console.log(hashedRef.toString());
// ==> "/https/io.datasync.orange.com/contacts//friends/pR/3a/fH/john"
createUser(details, onCompleteopt) → {Promise}
Creates a new Webcom account bound to an email/password identity.
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
details |
Object | The details associated with the account to create. Properties
|
|||||||||||||||||
onComplete |
Webcom.authCallback |
<optional> |
Callback function to be called when the account creation
completes. |
- Deprecated:
- use
addAccount()
instead
- use
Returns:
The account creation is performed asynchronously, and its result may be caught by assigning a
resolve and/or a reject callback to the returned Promise
. The resolve (resp.
reject) callback is called with one argument filled with the same value than the auth
(resp. error
) parameter of authentication callbacks.
- Type
- Promise
Example
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// Create a new account for this application
ref.createUser({
email: "john@webcom.com",
password: "pwd"
}, function(error, auth) {
if (error) {
switch (error.code) {
case "EXISTING_IDENTITY":
console.log("Email already in use!");
break;
case "INVALID_EMAIL":
console.log("Email format is not valid!");
break;
default:
console.log("Account creation failed: " + error.message);
}
} else {
console.log("Successfully created account with UID:" + auth.uid + ", it must be confirmed before use!");
}
});
endAt(nameopt) → {api.Query}
Creates a Query object that returns children beginning at a specific ending point.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
String |
<optional> |
The child name to end at |
- Version:
- 2.2+
- Inherited From:
Returns:
- Generated query
- Type
- api.Query
Example
// Get a reference to friends
var ref = new Webcom("/base/contacts/friends");
// Display friends whoses names (keys) come before john
ref.endAt("john").on("value", function(snapshot){
console.log(snapshot.val());
});
equalTo(nameopt) → {api.Query}
Creates a Query object that returns children matching a specific value.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
String |
<optional> |
The child name to match for. |
- Version:
- 2.2+
- Inherited From:
Returns:
- Generated query
- Type
- api.Query
Example
// Get a reference to friends
var ref = new Webcom("/base/contacts/friends");
// Display friends whoses names (keys) is exactly john
ref.equalTo("john").on("value", function(snapshot){
console.log(snapshot.val());
});
goOffline()
Forces disconnection of the websocket managed by this Webcom
instance from the Webcom server and
disables the retry connection feature. The websocket will be not restablished until Webcom#goOnline
is
called.
goOnline()
Force reconnection of the websocket managed by this Webcom
instance to the Webcom server and enables
the retry connection feature.
limit(lim) → {api.Query}
Create Query object to limit number of children
Parameters:
Name | Type | Description |
---|---|---|
lim |
Number | Number of items |
- Version:
- 2.2+
- Inherited From:
Returns:
- new limited
Query
object.
- Type
- api.Query
Examples
Last
// Get a reference to friends
var ref = new Webcom("/base/contacts/friends");
var lastfriends = ref.endAt().limit(5);
// Display the 5 last friends
lastfriends.on("value", function(snapshot){
console.log(snapshot.val());
});
First
// Get a reference to friends
var ref = new Webcom("/base/contacts/friends");
var lastfriends = ref.startAt().limit(5);
// Display the 5 first friends
lastfriends.on("value", function(snapshot){
console.log(snapshot.val());
});
First starting at some key
// Get a reference to friends
var ref = new Webcom("/base/contacts/friends");
var lastfriends = ref.startAt("john").limit(5);
// Display friends whoses names (keys) come after john display the following 5
lastfriends.on("value", function(snapshot){
console.log(snapshot.val());
});
logout(onCompleteopt)
Signs the currently authenticated user out.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
onComplete |
function |
<optional> |
A 1-argument callback function that is called after the log-out, which receives an
|
Example
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// Register an authentication callback
ref.resume(myAuthCallback);
// Log the current user out ==> ends up in calling myAuthCallback
ref.logout(function (error) {
if (error) {
console.log("The log-out failed: " + error.message);
} else {
console.log("Logged out successfully!");
}
});
name() → {String}
Returns the name of the data node this Webcom
instance refers to.
Returns:
The name of the data node or null
if this instance refers to the root of the application
- Type
- String
Example
// Get a reference to the "john" data node
var ref = new Webcom("contacts").child("friends/john");
console.log(ref.name());
// ==> "john"
console.log(ref.child("name").name());
// ==> "name"
console.log(ref.root().name());
// ==> null
off(eventTypeopt, callbackopt, contextopt)
Unwatches data changes at the data node referred to by this Query
instance.
It actually unregisters a callback function (or all ones) previously registered on the data node referred to
by this Query
instance using the on()
method.
Note: unregistering all callback functions attached to a data node doesn't unregister callback functions
attached to its child nodes. The off()
method must be called on all the data nodes that the
on()
method has been called on.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
eventType |
string | Array.<string> |
<optional> |
The type of event to unwatch. It can be either a single value among |
callback |
api.Query~watchCallback |
<optional> |
Reference to the callback function to unregister (previously passed
to or returned by the |
context |
Object |
<optional> |
Context object for callback binding, passed to previous call to
|
- Inherited From:
Examples
Unregister a single callback
// Get a reference to the "friends" node of the "contacts" application
var ref = new Webcom("contacts").child("friends");
var onChange = ref.on("value", function(snapshot) {
// your handler
});
// later...
ref.off("value", onChange);
Unregister several callbacks
// Get a reference to the "friends" node of the "contacts" application
var ref = new Webcom("contacts").child("friends");
var onChanged = function(snapshot) {
// your first handler
});
var onAdded = function(snapshot) {
// your second handler
});
ref.on("value", onChanged);
ref.on("child_added", onAdded);
// later...
// Unregister all callback functions from ref...
ref.off();
// ...or all callback function associated to a specific kind of event
ref.off("value"); // onChanged is unregistered while onAdded remains registered
on(eventType, callback, cancelCallbackopt, contextopt) → {api.Query~watchCallback}
Watches data changes at the data node referred to by this Query
instance.
It actually registers a callback function on the data node referred to by this Query
instance
for a given type of event to watch or a given set of types of events to watch.
The specified callback may be called initially (when the on
method is called) with respect to the data
currently stored at this node (depending on the requested event type). And then, it will be called whenever
the data change. The notifications can be stopped using the off()
method.
Several kinds of callback are available depending on the kind of event to watch:
-
value
eventThe callback is called initially once with the current data.
It is then called again each time the data (stored a this node or any sub-node) change.The
DataSnapshot
passed to the callback refers to the new data stored at the data node referred to by thisQuery
instance.
TheprevChildName
argument passed to the callback is not used and remains undefined. -
child_added
eventThe callback is called initially once for each current child node of this node.
It is then called again each time a new data child is added to this node.The
DataSnapshot
passed to the callback refers to the new data stored at the corresponding child data node.
TheprevChildName
argument passed to the callback refers to the name of the sibling child node that precedes the added child node in the data tree structure (following key order), ornull
if the added child node is the first child of the data node referred to by thisQuery
instance. -
child_changed
eventThe callback is not initially called.
It is then called each time the value of one of the data children changes (excluding child additions and removals).The
DataSnapshot
passed to the callback refers to the new data stored at the corresponding child data node.
TheprevChildName
argument passed to the callback refers to the name of the sibling child node that precedes the changed child node in the data tree structure (following key order), ornull
if the changed child node is the first child of the data node referred to by thisQuery
instance. -
child_removed
eventThe callback is not initially called.
It is then called each time one of the data children is removed (or its value becomesnull
or it loses all of its children).The
DataSnapshot
passed to the callback refers to the old data stored at the corresponding child data node.
TheprevChildName
argument passed to the callback is not used and remains undefined.
Note: data is first synchronized locally, so that if you set (using the set()
method) some
data locally after registering a watch callback, this callback will be called immediately. Later, when the
synchronization with the server occurs, the callback will be possibly called again for data update, or even for
data deletion if the synchronization fails.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
eventType |
string | Array.<string> | The type of event to watch. It can be either a single value among |
|
callback |
api.Query~watchCallback | Callback function called when the specified event occurs (see above). |
|
cancelCallback |
api.Query~cancelCallback |
<optional> |
Callback function called when the authenticated user loses
read permission at the data node referred to by this
|
context |
Object |
<optional> |
Context object for callback binding |
- Inherited From:
Returns:
A reference to the callback function provided in the callback
parameter. It
is intended to be passed to the off()
method, especially when the callback
parameter is
given as an inline function.
Examples
Value
// Get a reference to the "friends" node of the "contacts" application
var ref = new Webcom("contacts").child("friends");
// This callback is called only when some data change under the "friends" node
ref.on("value", function(snapshot) {
console.log("New friends value is: " + JSON.stringify(snapshot.val()));
});
Child Added
// Get a reference to the "friends" node of the "contacts" application
var ref = new Webcom("contacts").child("friends");
// This callback is called only when a new child is added to the "friends" node
ref.on("child_added", function(snapshot, prevChildName) {
console.log("New child for friends: " + snapshot.name() + " -> " + JSON.stringify(snapshot.val()));
if (prevChildName) {
console.log("This new child comes after " + prevChildName);
} else {
console.log("This new child is the only one");
}
});
Child Changed
// Get a reference to the "friends" node of the "contacts" application
var ref = new Webcom("contacts").child("friends");
// This callback is called only when the value of an existing child of the "friends" node changes
ref.on("child_changed", function(snapshot, prevChildName) {
console.log("The " + snapshot.name() + " child has changed value to: " + JSON.stringify(snapshot.val()));
if (prevChildName) {
console.log("This child comes after " + prevChildName);
} else {
console.log("This child is the only one");
}
});
Child Removed
// Get a reference to the "friends" node of the "contacts" application
var ref = new Webcom("contacts").child("friends");
// This callback is called only when a child of the "friends" node is removed
ref.on("child_removed", function(snapshot) {
console.log("The " + snapshot.name() + " child has been removed, its value was: " + JSON.stringify(snapshot.val()));
});
once(eventType, callback, cancelCallbackopt, contextopt)
Watches the next data change at the data node referred to by this Query
instance.
This method is similar to on()
, except it automatically unregisters the given callback
function from the data node referred to by this Query
instance as soon as it is called.
In other words, the given callback function will be called no more than once.
With the value
event type, this method makes it possible to read (asynchronously) the data stored at a given
data node.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
eventType |
String | The type of event to watch. It can be either |
|
callback |
api.Query~watchCallback | Callback function called when the specified event occurs. |
|
cancelCallback |
api.Query~cancelCallback |
<optional> |
Callback function called when the authenticated user loses
read permission at the data node referred to by this
|
context |
Object |
<optional> |
Context object for callback binding |
- Inherited From:
Examples
// Get a reference to the "friends" node of the "contacts" application
var ref = new Webcom("contacts").child("friends");
// Read the value of the "friends" node
ref.once("value", function(snapshot) {
console.log("The value of the " + snapshot.name() + " node is: " + JSON.stringify(snapshot.val()));
});
// Get a reference to the "friends" node of the "contacts" application
var ref = new Webcom("contacts").child("friends");
// Check the read permission on the "friends" node
ref.once("value", function() {}, function(error) {
console.log("Insufficient read permission");
});
onDisconnect() → {api.OnDisconnect}
Gets an instance of OnDisconnect
, which refers to the data node referred to by this
Webcom
instance.
Returns:
- Type
- api.OnDisconnect
Example
// Get a reference to the "john" data node
var ref = new Webcom("contacts").child("friends/john");
// Create a disconnection listener
var disconnectRef = ref.child("online").onDisconnect().set(false);
parent() → {Webcom}
Gets a Webcom
instance that refers to the parent data node of the one referred to by this Webcom
instance.
Returns:
A parent instance of this Webcom
instance or null
if the data node referred to by
this Webcom
instance has no parent.
- Type
- Webcom
Example
// Get a reference to the "friends" data node
var ref = new Webcom("contacts").child("friends");
var contactsRef = ref.parent();
console.log(contactsRef.toString());
// ==> "/https/io.datasync.orange.com/contacts/"
pathString() → {String}
Returns the path of the data node represented by this instance of Query
.
- Version:
- 2.6.2+
- Inherited From:
Returns:
The path of this data node, with path levels separated by "/" (slashes)
- Type
- String
persist()
Persists the data currently stored in the client local cache.
This method has no effect if the Webcom
instance that first established the connection to the Webcom
server was created with the {persist: false}
option (in this case, the method returns false
).
This data will the be restored in a further client establishing its first connection to the Webcom server
passing the {persist: true}
option to the Webcom
constructor.
- Version:
- 2.6+
Returns:
true
if the data has been persisted or false
otherwise
push(valueopt, onCompleteopt) → {Webcom}
Adds a new child at the data node referred to by this Webcom
instance.
Its key is automatically generated, so that it is unique and lexicographically ordered after any previously
generated key.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
value |
Object | String | Number | Boolean | null |
<optional> |
The value of the new child to add. If not set, the new child is created without value. |
onComplete |
function |
<optional> |
A 1-argument callback function called after synchronization with the server. Its
argument is filled with an |
Returns:
A Webcom
instance referring to the added child.
- Type
- Webcom
Examples
// Get a reference to the "john" data node
var ref = new Webcom("contacts").child("friends/john");
// Create a randomly generated unique child
var refPhone = ref.push();
console.log(refPhone.toString());
// ==> "/https/io.datasync.orange.com/contacts//friends/john/-Jjj6vK9L0NAd9JHusrd"
// Set a value on this new child
refPhone.set({number : "0123456789"});
var ref = new Webcom("contacts").child("friends/john");
// The same as the previous example, in a single operation
ref.push({number : "0123456789"});
var ref = new Webcom("contacts").child("friends/john");
// The same as the previous example, with a completion callback
ref.push({number : "0123456789"}, function(error) {
if (error) {
console.log("synchronisation error:", error);
} else {
console.log("synchronisation ok.");
}
});
ref() → {Webcom}
- Inherited From:
Returns:
A Webcom
reference.
- Type
- Webcom
Example
// Get a reference to the "friends" data node
var ref = new Webcom("contacts").child("friends");
var query = ref.limit(2);
var refSameLocation = query.ref(); //ref === refSameLocation
registerAuthCallback(callback) → {Webcom.authCallback}
Adds an authentication callback function to the set of functions subscribed to changes of the authentication
state. Contrary to the Webcom#addAuthCallback
method, this method doesn't initially call the added
callback function with the current authentication state.
Parameters:
Name | Type | Description |
---|---|---|
callback |
Webcom.authCallback | the callback function to add. |
- Version:
- 2.1+
- Deprecated:
- use
Webcom#addAuthCallback
instead
- use
Returns:
the added callback function.
- Type
- Webcom.authCallback
remove(onCompleteopt)
Removes data at the data node referred to by this Webcom
instance.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
onComplete |
function |
<optional> |
A 1-argument callback function called after synchronization with the server. Its
argument is filled with an |
Example
// Get a reference to the "john" data node
var ref = new Webcom("contacts").child("friends/john");
// Remove all data from the "john" data node
ref.remove();
removeAccount() → {Promise}
Deletes the Webcom account associated with the currently authenticated identity. This method deletes also all the identities associated with this account. In order to succeed, this sensible operation requires a fresh authentication. If the current authentication token is too old, then the operation fails, in this case you have to refresh the user authentication by authenticating her⋅him afresh explicitly before renewing the operation.
- Version:
- 2.6.2+
- See:
Returns:
The identity deletion is performed asynchronously, and its result may be caught by assigning a
resolve and/or a reject callback to the returned Promise
. In case of succes, the
resolve callback is called with no argument. In case of failure, the reject callback is
called with the reason of the failure.
- Type
- Promise
removeAuthCallback(callbackopt) → {boolean}
Removes an authentication callback function from the set of functions subscribed to changes of the authentication state. As soon as this method returns, further changes of the authentication state will no longer result in calling the specified callback function(s).
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
Webcom.authCallback |
<optional> |
the callback function to remove. Note that it must be a reference to
a function previously added with |
- Version:
- 2.8+
- See:
Returns:
true
if the callback function to remove was found, or false
otherwise
- Type
- boolean
removeIdentity() → {Promise}
Deletes the currently authenticated identity. This method doesn't delete the associated Webcom account unless there is no other identity associated with it. In order to succeed, this sensible operation requires a fresh authentication. If the current authentication token is too old, then the operation fails, in this case you have to refresh the user authentication by authenticating her⋅him afresh explicitly before renewing the operation.
- Version:
- 2.6.2+
- See:
Returns:
The identity deletion is performed asynchronously, and its result may be caught by assigning a
resolve and/or a reject callback to the returned Promise
. In case of succes, the
resolve callback is called with no argument. In case of failure, the reject callback is
called with the reason of the failure.
- Type
- Promise
removeUser(email, password, onCompleteopt) → {Promise}
Removes an existing email-based identity.
Warning: this method doesn't remove the associated Webcom account.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
email |
String | The email of the identity to remove |
|
password |
String | The password of the identity to remove |
|
onComplete |
function |
<optional> |
A 1-argument callback function that is called after the email/password identity
removal, which receives an |
- Deprecated:
- use
Webcom#removeAccount
orWebcom#removeIdentity
instead
- use
Returns:
- Type
- Promise
Example
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// Remove the "john@webcom.com" identity
ref.removeUser("john@webcom.com", "pwd")
.then(function() {
console.log("Identity removed!");
}).catch(function(error) {
switch (error.code){
case "INVALID_CREDENTIALS":
console.log("The identity or the password is invalid!");
break;
default:
console.log("The identity removal failed: " + error.message);
}
});
resume(callbackopt)
Resumes the authentication state (based on records from the local storage).
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
Webcom.authCallback |
<optional> |
If set, registers additionally an authentication callback function to be
called each time the authentication state changes. |
- Deprecated:
- This method no longer needs to be called
root() → {Webcom}
Gets a Webcom
instance that refers to the data tree root of current Webcom application.
Returns:
A Webcom
instance that refers to the root data node
- Type
- Webcom
Example
// Get a reference to the "john" data node
var ref = new Webcom("contacts").child("friends/john");
var rootRef = ref.root();
console.log(rootRef.toString());
// ==> "/https/io.datasync.orange.com/contacts/"
sendConfirmationEmail(email, onCompleteopt) → {Promise}
Sends an email with a link to confirm an email/password identity. This method is particularly useful when the
user has not received the initial confirmation email sent when creating her/his account (using the Webcom#createUser
method.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
email |
String | The email address of the identity to confirm, which the email must be send to |
|
onComplete |
function |
<optional> |
A 1-argument callback function that is called after the sending of the
confirmation email, which receives an |
- Deprecated:
- use
Webcom#sendVerificationCode
instead
- use
Returns:
- Type
- Promise
Example
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// Send a confirmation email
ref.sendConfirmationEmail("john@webcom.com", function(error) {
if (error) {
switch (error.code) {
case "INVALID_CREDENTIALS":
console.log("The specified identity is invalid!");
break;
case "ALREADY_VERIFIED_IDENTITY":
console.log("The specified identity is already confirmed");
break;
default:
console.log("The sending of the confirmation email failed: " + error.message);
}
} else {
console.log("Confirmation email successfully sent!");
}
});
sendOtp(provider, id, messageopt) → {Promise}
Sends a one time password (OTP) to a user through a given identity. This method is particularly useful before
calling the Webcom#authInternally
method, which may requires a temporary password to complete depending
on the chosen identity provider.
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
provider |
string | The provider associated with the identity, which to send the OTP to. Currently, possible
values are |
|||||||||||||||||||||
id |
string | The identifier of the identity, which to send the OTP to |
|||||||||||||||||||||
message |
object |
<optional> |
The parameters to apply to OTP message to be sent Properties
|
- Version:
- 2.6.2+
Returns:
A promise that will be resolved once the OTP successfully sent. In case of success, the
resolve callback is called with one parameter equal to the sent OTP identifier or challenge
(given as a string). This identifier is required for further methods such as
Webcom#authInternally
.
- Type
- Promise
sendPasswordResetCode(provider, id, messageopt) → {Promise}
Sends a code to the user, which allows her⋅him to reset her⋅his password for a given DataPulse internally
manged identity. Currently, only email-based identities are supported (parameter provider
equal to
"password"
).
Parameters:
Name | Type | Attributes | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
provider |
string | The internal provider of the identity, which to send the password reset code to.
Currently, only |
||||||||||||||||
id |
string | The identifier of the identity, which to send the password reset code to. |
||||||||||||||||
message |
<optional> |
The message template to use to send the password reset code Properties
|
- Version:
- 2.6.2+
Returns:
A promise that is resolved once the password reset code has been sent
- Type
- Promise
sendPasswordResetEmail(email, onCompleteopt) → {Promise}
Sends an email to the user of an email/password identity, with a link to reset the associated password. This method is intended to be used when the user has lost her/his password.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
email |
String | The email of the identity to reset |
|
onComplete |
function |
<optional> |
A 1-argument callback function that is called after the sending of the password
reset email, which receives an |
- Deprecated:
- use
Webcom#sendPasswordResetCode
instead
- use
Returns:
- Type
- Promise
Example
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// Send a password reset email
ref.sendPasswordResetEmail("john@webcom.com", function(error) {
if (error) {
switch (error.code) {
case "INVALID_CREDENTIALS":
console.log("The specified identity is invalid!");
break;
default:
console.log("The sending of the password reset email failed: " + error.message);
}
} else {
console.log("Reset password email successfully sent!");
}
});
sendVerificationCode(provider, id, messageopt) → {Promise}
Sends a code to the user, which makes it possible to verify one of her⋅his unverified identities internally managed by DataPulse (i.e. email- and phone-based identities). This method fails on an already verified identity.
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
provider |
string | The internal provider that manages the identity to verify. Current supported values are
|
|||||||||||||||||||||
id |
string | The identifier of the identity to verify. It is either an email address (for email-based identities) or a MSISDN without the "+" prefix (for phone-based identities). |
|||||||||||||||||||||
message |
<optional> |
The message template to use to send the verification code to the user Properties
|
- Version:
- 2.6.2+
Returns:
A promise that will be resolved once the verification message successfully sent. In case of
success, the resolve callback is passed a "challenge" string that must be used to
eventually verify the identity using the Webcom#verifyIdentity
method
- Type
- Promise
set(newVal, onCompleteopt)
(Over)writes data at the data node referred to by this Webcom
instance.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
newVal |
Object | String | Number | Boolean | null | The value to write. If it is an |
|
onComplete |
function |
<optional> |
A 1-argument callback function called after synchronization with the server. Its
argument is filled with an |
Examples
Write a single value
// Get a reference to the "john" data node
var ref = new Webcom("contacts").child("friends/john");
// Overwrite first name and last name with "John" and "Doe" values
ref.child("name/first").set("John");
ref.child("name/last").set("Doe");
Write an object
// Get a reference to the "john" data node
var ref = new Webcom("contacts").child("friends/john");
// Define a callback to log the result
var onFinish = function(error) {
if(error) {
console.log("There is a problem...");
} else {
console.log("It's OK!");
}
};
// Overwrite the "name" data node atomically
ref.child("name").set({first:"John",last:"Doe"}, onFinish);
setPriority(priority, onCompleteopt)
Setting priority at current location.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
priority |
String | Number | priority value for new value |
|
onComplete |
function |
<optional> |
function called after synchronization with the server. An error can be passed in arguments. |
- Version:
- 9999.0+
Example
// Get a reference to john
var ref = new Webcom("/contacts/friends/john");
// Priority is changed to 50
ref.setPriority(50);
setWithPriority(newVal, newPriority, onCompleteopt)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
newVal |
Object | String | Number | Boolean | null | The value to write (behaves like in |
|
newPriority |
String | Number | The priority value to associate with the new value |
|
onComplete |
function |
<optional> |
A 1-argument callback function called after synchronization with the server. Its
argument is filled with an |
- Version:
- 9999.0+
- Deprecated:
Example
// Get a reference to the "john" data node
var ref = new Webcom("contacts").child("friends/john");
var johnValue = {
name: {
first: "John",
last: "Doe"
},
numberOfCalls: 100
};
// "name" and "numberOfCalls" data nodes are written, with 100 as associated priority, so that the "john" data
// node will be ordered using this value with respect to other sibling data nodes
ref.setWithPriority(johnValue, 100);
shouldBeOnline() → {boolean}
Returns the current expected state of the Datasync websocket: true
if it should be online (whatever the actual
network state) or false
if it should be offline.
The actual state of the Datasync websocket may be monitored by subscribing to the ".info/connected" virtual node
using the on()
method.
- Version:
- 2.6.2+
Returns:
the current expected state of the Datasync websocket
- Type
- boolean
startAt(nameopt) → {api.Query}
Creates Query object that returns children beginning at a specific starting point.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
String |
<optional> |
The child name to start at |
- Version:
- 2.2+
- Inherited From:
Returns:
- Generated query
- Type
- api.Query
Example
// Get a reference to friends
var ref = new Webcom("/base/contacts/friends");
// Display friends whoses names (keys) come after john
ref.startAt("john").on("value", function(snapshot){
console.log(snapshot.val());
});
toString() → {String}
Returns the absolute URL of the data node referred to by this Webcom
instance.
Returns:
The absolute URL of this Webcom
instance
- Type
- String
Example
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
console.log(ref.toString());
// ==> "/https/io.datasync.orange.com/contacts/"
var johnRef = ref.child("friends/john");
console.log(johnRef.toString());
// ==> "/https/io.datasync.orange.com/contacts//friends/john"
var alfredRef = ref.child("friends/alfred:54");
console.log(alfredRef.toString());
// ==> "/https/io.datasync.orange.com/contacts//friends/alfred%3A54"
transaction(transactionUpdate, onCompleteopt, optionsopt)
Writes data atomically within a transaction at the data node referred to by this Webcom
instance. This
means the data will be actually written only if the data node is not modified by another client during the
transaction. Otherwise, the transaction is retried (up to 25 times) until it can be completed safely without
conflict.
Parameters:
Name | Type | Attributes | Default | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
transactionUpdate |
function | A 1-argument function that receives the current value of the data node and
is expected to return either an undefined value to abort the transaction, or
the new value to overwrite at the data node. |
|||||||||||||||||
onComplete |
function |
<optional> |
A 3-argument function that is called after the transaction execution whatever its outcome (completed or aborted):
|
||||||||||||||||
options |
object |
<optional> |
{} |
Properties
|
- Version:
- 1.1+
Examples
Increment the value of a data node
// Get a reference to the "john" data node
var ref = new Webcom("contacts").child("friends/john");
ref.child("numberOfCalls").transaction(function(current) {
if (current === null) {
return 0; // initial value
} else {
return current + 1; // next values
}
});
Write some data only if there is none
// Get a reference to the "john" data node
var ref = new Webcom("contacts").child("friends/john");
ref.transaction(function(current) {
if (current === null) { // there is no data at the "john" data node => set some
return {
name: {
first: "John",
last: "Doe"
}
};
} else { // there is already some data => abort transaction
console.log("John already exists!");
return;
}
}, function(error, committed, snapshot){
if (error) {
console.log("An unexpected error occurred:", error);
} else if (!committed) {
console.log("Transaction aborted (John already exists)");
} else {
console.log("Transaction completed (John added)");
}
console.log("John:", snapshot.val());
});
unregisterAuthCallback(callbackopt) → {boolean}
Removes an authentication callback function from the set of functions subscribed to changes of the authentication state.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
Webcom.authCallback |
<optional> |
- Version:
- 2.1+
- Deprecated:
- use
Webcom#removeAuthCallback
instead
- use
Returns:
- Type
- boolean
update(newVal, onCompleteopt)
Updates data at the data node referred to by this Webcom
instance.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
newVal |
Object | The children to add to the current data node, given as a json |
|
onComplete |
function |
<optional> |
A 1-argument callback function called after synchronization with the server. Its
argument is filled with an |
Examples
// Get a reference to the "lords" data node
var ref = new Webcom("got").child("lords");
// Update first name and last name without deleting other existing children of "id56".
ref.child("id56").update({first:"John",last:"Snow"});
// Thus: if data at ref.child('id56') was {first:"Ned",last:"Stark",status:"King of the North"}
// it is now: {first:"John",last:"Snow",status:"King of the North"}.
Warning about update depth
ref.child('id57').update({ name: { first:'Alice' } })
//is exactly equivalent to
ref.child('id57').child("name").set({ first:'Alice' })
// Thus: if data at ref.child('john') was {first:"John",last:"Doe"}
// it is now: { first:'Alice' }. Note that last field is deleted
//Other example:
ref.child('id57').update({ name: { first:'John' } , age:43})
//is equivalent to
ref.child('id57').child("name").set({ first:'John' })
//PLUS
ref.child('id57').child("age").set(43)
//except that update performs 2 operations atomically.
updateEmailPasswordProfile(pathopt, data, onCompleteopt) → {Promise}
Updates some data within the profile associated with the currently authenticated email/password identity.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
path |
string |
<optional> |
The path within the profile where to update the data. |
data |
object | The data to update within the profile (as a JSON object). |
|
onComplete |
function |
<optional> |
A 1-argument callback function that is called after the profile update completion,
which receives an |
- Deprecated:
- use
Webcom#updateIdentityProfile
instead
- use
Returns:
A promise that will be resolved (without passing any argument) as soon as the update operation completes successfully, or rejected otherwise (with the failure reason as argument)
- Type
- Promise
Example
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// We must be authenticated...
ref.authWithPassword({email:"john@webcom.com", password:"mypassword"})
// ...before updating the identity profile
.then(auth => ref.updateEmailPasswordProfile({age: 42}))
.then(() => console.log("profile updated!")
.catch(error => console.log("profile update failure:", error);
updateIdentityProfile(pathopt, data) → {Promise}
Updates some data within the profile associated with the currently authenticated email- or phone-based identity. This method fails if no user is currently authenticated or if the currently authenticated user is not identified with an email- or phone-based identity.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
path |
string |
<optional> |
The path within the profile where to update the data. |
data |
object | The data to update within the profile (as a JSON object). |
- Version:
- 2.6.2+
Returns:
A promise that will be resolved (without passing any argument) as soon as the update operation completes successfully, or rejected otherwise (with the failure reason as argument)
- Type
- Promise
Example
// Get a reference to the "contacts" application
var ref = new Webcom("contacts");
// We must be authenticated...
ref.authWithPassword({email:"john@webcom.com", password:"mypassword"})
// ...before updating the identity profile
.then(auth => ref.updateIdentityProfile({age: 42}))
.then(() => console.log("profile updated!")
.catch(error => console.log("profile update failure:", error);
useCurrentContextForNextAuthOperation()
After this method is called, the next authentication-related operation will be performed in the context of the current authentication state. An authentication request will end in a multi-factor token (including at least the identity currently signed in). An identity creation request will add a new identity to the currently signed in Webcom account.
Once the next authentication-related operation performed, the effect of this method is forgotten, that is,
further authentication-related operations are executed starting from an empty authentication context event if a
user is authenticated.
If you need to cancel the effect of this method before the next authentication-related operation, simply
call the Webcom#useNewContextForNextAuthOperation
method.
- Version:
- 2.8+
useNewContextForNextAuthOperation()
Cancels the effect of the Webcom#useCurrentContextForNextAuthOperation
method. Once called, the next
authentication-related operation will be performed without any context of authentication state, even if a user
is currently signed in.
- Version:
- 2.8+
verifyIdentity(provider, id, data) → {Promise}
Verifies an identity newly created using the Webcom#addIdentity
method. If the verification is
successful, this method also signs the user in with this identity.
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
provider |
string | The internal provider used to create the identity to verify. Currently only |
||||||||||||
id |
string | The identifier of the identity to verify. For email-based identities, it is an email address. For phone-based identities, it is a MSISDN (without the "+" prefix). |
||||||||||||
data |
object | The verification data. Properties
|
- Version:
- 2.6.2+
Returns:
The authentication following the verification is performed asynchronously, and its result may
be caught by assigning a resolve and/or a reject callback to the returned Promise
.
The resolve (resp. reject) callback is called with one argument filled with the same value
than the auth
(resp. error
) parameter of
authentication callbacks.
- Type
- Promise
Type Definitions
authCallback(error, auth)
A callback function that is called when the authentication state changes.
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
error |
Error | If not Properties
|
||||||||||||
auth |
Object | Relevant only when Properties
|