0

This is my code for app component ts where I want to communicate to a websocket (for which I installed sockjs-client and stompjs). I don't know how to solve the error.

import { Component } from '@angular/core';
import * as Stomp from 'stompjs';
import * as SockJS from 'sockjs-client';
import $ from 'jquery';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  private serverUrl = 'http://localhost:8080/socket'
  private title = 'WebSockets chat';
  private stompClient;

  constructor(){
    this.initializeWebSocketConnection();
  }

  initializeWebSocketConnection(){
    let ws = new SockJS(this.serverUrl);
    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);
        }
      });
    });
  }

  sendMessage(message){
    this.stompClient.send("/app/send/message" , {}, message);
    $('#input').val('');
  }

}

This is the following error that I perform ng-serve:

ERROR in ./node_modules/stompjs/lib/stomp-node.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\drnic\Desktop\Messaging app\Front-end\bantachat2\node_modules\stompjs\lib'
1
  • I have installed stompjs and sockjs-client Commented Jan 6, 2020 at 14:38

2 Answers 2

2

Install dependency package 'net' using command

'npm i net -S'

Sign up to request clarification or add additional context in comments.

Comments

0

Node stomp is a serverside node.js package. This means that is expects the entire node.js stack to be installed and used, which isn't the case in an angular application.

That's why the problem is that the library can't find the net dependency. So basically you have installed the wrong thing.

Check a very similar issue here

https://github.com/jmesnil/stomp-websocket/blob/master/lib/stomp.js

Is the browser file. The node one will only work in a NodeJS Environment. It calls require using some Node modules.

Comments

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.