5

I am following this tutorial at the moment and I got to the point, where I have to instantiate my SockJS-Client inside my Angular5 Component.

Here is what I did in:

  1. I exectuted the following commands to install the required libraries:

    npm install stompjs
    npm install sockjs-client
    npm install jquery
    
  2. I imported the libraries to my component like so:

    import { Stomp } from 'stompjs';
    import { SockJS } from 'sockjs-client';
    import $ from 'jquery';
    
  3. Finally I tried to instantiate SockJS:

    constructor(){
        this.initializeWebSocketConnection();
    }
    
    initializeWebSocketConnection(){
        let ws = new SockJS(this.serverUrl);  //<- This is the line causing the error
        this.stompClient = Stomp.over(ws);
        let that = this;
        this.stompClient.connect({}, function(frame) {
            that.stompClient.subscribe("/chat", (message) => {
                if(message.body) {
                    $(".chat").append("<div class='message'>"+message.body+"</div>")
                    console.log(message.body);
                }
            });
        });
    }
    

The error I am getting is:

ERROR Error: Uncaught (in promise): TypeError: sockjs_client_1.SockJS is not a constructor

I can't find anything about this problem.

1 Answer 1

10

Try either:

import * as SockJS from 'sockjs-client';

or

import SockJS from 'sockjs-client';
Sign up to request clarification or add additional context in comments.

2 Comments

The first option worked for me, thanks a lot!!! Can you explain why my option led to that Error?
All I can say is that's the way I saw the import done in another example of using SockJS. I guess it's just a matter of how the module is defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.