Before you begin

When you first use Resonance SDK, you have to create account at console.getresonance.net and register an application to get access to API key.

Note: You should use the same API key on all platforms in order to allow cross platform user matching.

Loading JavaScript SDK

To load the Resonance JavaScript SDK, use a script tag like the one in the following example:

<script async defer src="https://cdn.getresonance.net/sdk/js/resonance-1.0.2.js"></script>
The URL contained in the script tag is the location of a JavaScript file that loads all of the symbols and definitions you need for using the Resonance JavaScript SDK.

The async attribute lets the browser render the rest of your website while the SDK loads. When the SDK is ready, it will call the window.resonanceAsyncInit function.

Initializing Resonance

Before using Resonance API you must initialize the SDK using the API key of your application:

window.resonanceAsyncInit = function() {
  var resonance = new Resonance("DEMO_API_KEY");
};

Starting search

For devices to discover eachother, all of them must start a search. To do this you need to call resonance.startSearch(payload, callback) method.

You can omit callback argument in this case startSearch will return Promise.

To receive a notification when a device is located nearby or if there was an error, you need to subscribe on nearbyFound and searchStopped events.

Full example:

window.resonanceAsyncInit = function() {
  if(!Resonance.isCompatible()) {
    console.error('Your browser is not supported');
  }

  var resonance = new Resonance('DEMO_API_KEY');

  resonance.startSearch('Some useful data to broadcast', function(error) {
    if(error) {
      console.error(error.message);
    } else {
      // search started
    }
  });

  resonance.on('nearbyFound', function(nearby) {
    console.log(nearby.payload);
  });

  resonance.on('searchStopped', function(error) {
    if(error) {
      console.error(error.message);
    } else {
      // search was stopped normally
    }
  });
};

Stopping search

To stop the search, you should call resonance.stopSearch().

searchStopped event is emitted when the search is stopped by application or when search is stopped due to an error – such as when there is no connection with the server. If the argument error is equal to undefined, then the search has been stopped by the application. If not then there was an error.