Webcom is the Orange [Backend-as-a-Service](https://en.wikipedia.org/wiki/Mobile_Backend_as_a_service) / Serverless
solution. It provides integrated functions for:
- database,
- message exchange (publish / subscribe),
- notification on mobile devices,
- authentication federation,
- data exposure and access control.

This platform drastically reduces time and cost to implement and deploy mobile and web applications in production.

More information on [https://datasync.orange.com](https://datasync.orange.com).

#### [Webcom documentation](https://datasync.orange.com/doc)

#### [Webcom SDKs news on Plazza](https://plazza.orange.com/groups/webcom/projects/sdk-infos)

#### [Samples for Web Applications](https://datasync.orange.com/doc/tutorial-tips-examples.html)

#### [Changelog](https://datasync.orange.com/repository/webcom-sdk-js/3.2.0/doc/tutorial-changelog.html)

## Quick start

### 1. Create your own developer account on the [Webcom developer console](https://io.datasync.orange.com)

### 2. Create a Webcom application on the [Webcom developer console](https://io.datasync.orange.com)

### 3. Add the Webcom javascript library to your project

#### 3.a Web applications, directly
```html
<script type='text/javascript' src='https://cdn.jsdelivr.net/npm/webcom@3.2.0/webcom.js'></script>
```

#### 3.b Web applications, with [`npm`](https://www.npmjs.com/)

First install the Webcom package
```shell script
npm install webcom@3.2.0
```

And then reference the installed javascript library in your web application:
- either with a `script` tag
```html
<script type='text/javascript' src='<YOUR_LIBRARY_PATH>/webcom.js'></script>
```
- or with a javascript `import` directive
```javascript
import 'webcom/webcom.js';
```

#### 3.c Node.js applications

First install the Webcom package
```shell script
npm install webcom@3.2.0
```

And then load the javascript library in your Node.js application
```javascript
const Webcom = require('webcom');
```

### 4. Use Webcom in your code

#### 4.a Create a reference to your Webcom application
```javascript
const myApp = Webcom.App('<your-app>');
```

The Authentication and ServerlessDb services may then be accessed respectively through `myApp.authentication` and
`myApp.serverlessDb`.

#### 4.b Listen to data within your data tree
```javascript
const node = myApp.serverlessDb.rootNode.relativeNode("the/targeted/path");
node.subscribe(Webcom.Event.ValueChange, Webcom.Callback(snapshot => {
    // this callback is called each time the data in your app at "the/targeted/path" is updated
    console.info("data in my app is now:", snapShot.val());
}));
```

You can also send notifications to a webhook instead of a javascript callback (to do so, you must configure the
"`myWebhook`" webhook on the [Webcom developer console](https://io.datasync.orange.com)):
```javascript
node.subscribe(Webcom.Event.ValueChange, Webcom.Webhook("myWebhook", "aContext"));
```

#### 4.c Write data into your data tree
```javascript
node.set({foo: 'bar'})
        .then(() => console.info("write operation succeeded"))
        .catch(error => console.info("write operation failed:", error.message));
```

While the `set` method overwrites data, the `merge` one completes existing data:
```javascript
node.merge({baz: 42});
```
The whole data tree of your application is now
```json
{
 "the": {
  "targeted": {
   "path": {
    "foo": "bar",
    "baz": 42
   }
  }
 }
}
```

### 5. Run your application
Applications hosted by a web browser should run straightforward.

If you run a Node.js application, be careful to the network configuration: behind a company proxy, you just have to
set up usual environment variables `https_proxy` and `http_proxy` (or their uppercase counterparts):
```shell script
export http_proxy="http://my-proxy.my-company.com:8080"
```
Note that the `no_proxy` environment variable is also taken into account.
