Jakob Nielsen
Jakob Nielsen

Reputation: 5198

Trying to use stomp.js in angular2

I am trying to implement a Stomp Websocket client using stomp.js.

I am using angular2 with typescript and webpack and am really new to all of those technologies. My angular2 project was built on this seed: https://github.com/angular/angular2-seed

As a guide for implementing the stomp.js client I used https://github.com/sjmf/ng2-stompjs-demo

The error I am currently getting is the following:

?d41d:73 EXCEPTION: TypeError: Cannot read property 'client' of undefined in [null]

The error is happening in this method:

  public configure() : void {

    // Check for errors
    if (this.state.getValue() != STOMPState.CLOSED) {
      throw Error("Already running!");
    }

    let scheme : string = 'ws';
    if( AppSettings.IS_SSL ) {
      scheme = 'wss';
    }

    this.client = Stomp.client(
      scheme + '://'
      + AppSettings.HOST + ':'
      + AppSettings.WEBSOCK_PORT
      + AppSettings.WEBSOCK_ENDPOINT
    );

    this.client.heartbeat.incoming = AppSettings.HEARTBEAT;
  }

So Stomp seems to be undefined.

I am importing:

import {Stomp} from "stompjs";

I have installed stomp.js with npm like this

npm install --save stompjs

And my stompjs module looks like this:

declare module "stompjs" {

  export interface Client {
    heartbeat: any;

    debug(...args: string[]);

    connect(...args: any[]);
    disconnect(disconnectCallback: () => any, headers?: any);

    send(destination: string, headers?:any, body?: string);
    subscribe(destination: string, callback?: (message: Message) => any, body?: string);
    unsubscribe();

    begin(transaction: string);
    commit(transaction: string);
    abort(transaction: string);

    ack(messageID: string, subscription: string, headers?: any);
    nack(messageID: string, subscription: string, headers?: any);
  }

  export interface Message {
    command: string;
    headers: any;
    body: string;

    ack(headers?: any);
    nack(headers?: any);
  }

  export interface Frame {
    constructor(command: string, headers?: any, body?: string);

    toString(): string;
    sizeOfUTF8(s: string);
    unmarshall(datas: any);
    marshall(command: string, headers?, body?);
  }

  export interface Stomp {
    client: Client;
    Frame: Frame;

    over(ws: WebSocket);
  }
}

I think I am missing the connection between my module and the actual library, but I don't really know how to that and I can't figure it out from the github demo either.

Thanks in advance for any help!

Upvotes: 4

Views: 5286

Answers (2)

Deepak Kumar
Deepak Kumar

Reputation: 972

Using Stomp.js directly in Angular2 (or Angular4) is definitely non trivial.

For a proper Angular2 (and Angular4) type StompService using Observables please refer to https://github.com/stomp-js/ng2-stompjs.

There are sample apps for Angular2 and Agular4 as well.

Upvotes: 0

Valéry
Valéry

Reputation: 4708

Did you try to export a variable too alongside the interface?

export var Stomp: Stomp;

Upvotes: 1

Related Questions