AutobahnJS subscribe method error


i used AutobahnJS on angular4.

and i created autobahnService

this code for subscribe method in autobahnservice

public onopen() {

    this.connection.onopen = (session, details) => {

        this.session = session;



public subscribe(channel, event) {

    console.log('sub method');

    this.session.subscribe(channel, event).then(

        (sub) => {

            console.log('subscribed to topic');


        (err) => {

            console.log('failed to subscribe to topic', err);




i called it on app component within ngOnInit

this.autobahnService.connect(“ws://”, “realm1”);


function onevent(args) {

  this.counter = args[0];

  console.log("Event:", this.counter);


this.autobahnService.subscribe(‘com.example.oncounter’, onevent);

when i run server, i get some error msg…


ERROR TypeError: Cannot read property ‘subscribe’ of undefined
at AutobahnService.webpackJsonp…/…/…/…/…/src/app/services/autobahn.service.ts.AutobahnService.subscribe (autobahn.service.ts:76)
at AppComponent.webpackJsonp…/…/…/…/…/src/app/app.component.ts.AppComponent.ngOnInit (app.component.ts:35)
at checkAndUpdateDirectiveInline (core.es5.js:10894)
at checkAndUpdateNodeInline (core.es5.js:12382)
at checkAndUpdateNode (core.es5.js:12321)
at debugCheckAndUpdateNode (core.es5.js:13182)
at debugCheckDirectivesFn (core.es5.js:13123)
at Object.eval [as updateDirectives] (AppComponent_Host.html:1)
at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13108)
at checkAndUpdateView (core.es5.js:12288)
ERROR CONTEXT DebugContext_ {view: Object, nodeIndex: 0, nodeDef: Object, elDef: Object, elView: Object}