I have an example here for web3js 1.0.0.beta:
function handler (event) {
console.log(event.returnValues);
}
function errorCallback (err) {
console.error(err);
}
let subscription = contractObj.events.TestEvent().subscription;
subscription.on('data', handler).on('error', errorCallback);
/////// code to unsubscribe
subscription.unsubscribe(function (result) {
console.log(result)
});
///// code for usage class
class Listener {
constructor(event, handler) {
this.subscription = event;
this.subscription.on('data', handler).on('error', this.errorCallback);
}
errorCallback(err) {
console.log(err);
}
}
class Test {
constructor(contractObj) {
this.contractObj = contractObj;
this.createListener();
}\
createListener() {
let self = this;
this.listener = new Listener(this.contractObj.events.TestEvent(), function (event) {
self.returnValues = event.returnValues;
});
}
}