Chirp JavaScriptSDK V2.0

Dinu Chiriac
Chirp
Published in
3 min readFeb 22, 2017
ES2015

We are happy to announce the second major release of the Chirp JavaScript SDK.

Because JavaScript is evolving rapidly, we are moving forward with it and have developed a new version of the Chirp JS SDK to make developers lives easier. The biggest change is “promises” which will allow you to avoid one of the biggest problems with JavaScript — spaghetti callback code.

To keep consistency with other Chirp products like Android SDK or iOS SDK, JavaScript SDK v2.0.0 is now object oriented. There are other lovely improvements, namely:

  • We have a few different classes that manage the JavaScript SDK. The first is ChirpSDK() which is the main class used to instantiate the SDK, the second is Chirp() which is used to create chirps.
  • Each class has a promise property that you can use to get the creating state.
  • Each method of all classes returns a promise as well.
  • We implemented a new ChirpError class that you can use to identify errors more clearly.

To understand the difference, let’s go through a few examples of how the interface has changed since version 1.1.0.

Instantiate ChirpSDK

Instantiate the SDK and catch the authentication state with old SDK.

var chirpsdk = new ChirpSDK("<%= APP_KEY %>", function(err, res) {
//Catch authentication with callback function.
if (err) {
console.error(err);
return
}
console.log(res);
});

Instantiate the SDK and catch the authentication state with new SDK.

function success(auth_data) {
console.log("authenticated");
}
function error(error) {
console.error("Error: " + error.message);
}
var chirpsdk = new ChirpSDK("<%= APP_KEY %>");
chirpsdk.promise
.then(success)
.catch(error);

Encode an identifier

Encode an identifier with old JS SDK.

chirpsdk.encode(identifier, function (err, identifierEncoded) {
if (err) {
console.error(err);
}
else {
console.log(identifierEncoded);
}
});

Encode an identifier with new JS SDK.

function error(err) {
console.error(err);
}
function success(identifierEncoded) {
console.log(chirp.identifierEncoded);
}
chirp = new Chirp(identifier);
chirp.encode()
.then(success)
.catch(error);

Create chirp with associated data

Create chirp with associated data with old JS SDK.

chirpsdk.create({foo: 'bar'}, function (err, chirp) {
if (err) {
console.error(err);
} else {
console.log(chirp);
}
});

Create chirp with associated data with new JS SDK.

var chirp = new Chirp({foo: “bar”});
chirp.promise
.then(function() {console.log(“success”)})
.catch(function(err) {console.error(err)});

Create, encode and then getJsonData

Create, encode and then getJsonData with old JS SDK.

chirpsdk.encode(identifier, function(err, identifierEncoded) {
if (err) {
console.error(err);
} else {
console.log(identifierEncoded);
chirpSdk.get(identifier, function (err, data) {
if (err) {
console.error(err);
}
else {
console.log(data);
}
});
}
});

Create, encode and then getJsonData with new JS SDK.

chirp = new Chirp(identifier);
chirp.promise
.then(chirp.encode.bind(chirp))
.then(chirp.getJsonData.bind(chirp))
.then(function(){
console.log(chirp.identifierEncoded);
console.log(chirp.data);
}).catch(function(error) {
console.error(error);
});

Catch ChirpError

The error object returned in catch function is a “ChirpError” type so in case you have a promise chain, you can easily check where the error is coming from:

var chirp = new Chirp({foo: “bar”});
chirp.promise
.then(doSomething)
.then(doSomethingElse)
.catch(function(error) {
if (error.instanceOf(ChirpError)) {
console.log(“this is a ChirpError”);
}
});

ChirpSDK Class play a chirp.

Create and play a chirp with associated data with old JS SDK.

chirpsdk.create({foo: 'bar'}, function (err, chirp) {
if (err) {
console.error(err);
} else {
chirpsdk.chirp(chirp.identifierEncoded, function(err, res) {
if (err){
console.error(err)
} else {
console.log("done chirping");
});
}
});

Create and play a chirp with associated data with new JS SDK.

chirpsdk.chirp(new Chirp({foo: "bar"}))
.then(function() {
console.log("success");
}).catch(function(error){
console.error(error);
});

Currently JavaScript SDK v2.0.0 is in beta version however you can download it from the Chirp Admin Centre where you’ll find a demo project and a README file with a full documentaion.

Get started

To find out more about Chirp’s data-over-sound solutions, please visit us at Chirp.io or get in touch contact@chirp.io.

--

--