Tutorial: JavaScript

Quick Start JavaScript

This quickstart guide is for the JavaScript SDK. If you use Android, please refer to this article.

  1. Create your own account and application on the [[console]]

  2. If you use Node.JS, please refer to this article

  3. Add the javascript library to your HTML page:

    <script type='text/javascript' src='[[siteUrl]]/libjs/latest/webcom.js'></script>

    Or install package via npm and reference [[service]] library in your web application

    npm install webcom


    <script type='text/javascript' src='YOUR_LIBRARY_PATH/webcom.js'></script>

    or else with import:

import 'webcom/webcom.js';

or else you could add in your 'webpack.config.js' :

module.exports = {
[...]
  resolve: {
        alias: {
                webcom: 'webcom/webcom.js'
        }
  }
};

and then just import 'webcom' will work well.

  1. In javascript, create a Webcom reference to your [[service]] application:

    var myRef = new Webcom('<your-app>');
  2. Listen to changes:

    myRef.on('child_added', function(snapShot){
     // If another users call ref.set({foo: 'bar'}) then snapShot.val() will be equal to 'bar'
     // If another users call ref.push({foo: 'bar'}) then snapShot.val() will be equal to {foo: 'bar'}
     console.info(snapShot.val());
    });
  3. Write data to myRef:

    myRef.set({foo: 'bar'});

    or Create a new child data to myRef:

    var barRef = myRef.set({bar: 'baz'});