Catechacha
Catechacha

Reputation: 123

STOMPJS over and subscribe methods not working with Spring, SockJs, npm, webpack

I want to use websocket with SockJs and Stomp client-side, and Java server-side, in a Spring-boot web application. I have installed with npm sockjs-client and stompjs. My client-side code is:

    var SockJS = require('sockjs-client');
    var Stomp = require("stompjs");        
    var socket = new SockJS('/webSocketEndPoint');
          stompClient = Stomp.over(socket);
          stompClient.connect({}, function (frame) {
          stompClient.subscribe('/topic/notification', function (notificationBody) {
             alert(JSON.parse(notificationBody.body).content);
          });
        })

The methods over and subscribe are not working, so the websocket i want to build is not working. IntelliJ say me "Unresolved function or method over" and "Unresolved function or method subscribe ". I've included in my package.json stompjs and sockjs-client (correct version). The initial requires not makes me problem, but i'm not able to run this script..Someone can help me?! Thanks a lot

Upvotes: 0

Views: 3114

Answers (1)

Pavel Muravyov
Pavel Muravyov

Reputation: 86

Maybe because of wrong quotes in require("stompjs")? Here is my workin example:

let SockJS = require('sockjs-client');
const Stomp = require('stompjs');

function register(registrations) {
    let socket = SockJS('/webSocketEndPoint');
    let stompClient = Stomp.over(socket);
    stompClient.connect({}, function(frame) {
        registrations.forEach(function (registration) {
            stompClient.subscribe('/topic/notification', function (notificationBody) {
             alert(JSON.parse(notificationBody.body).content);
          });
        });
    });
}

Upvotes: 1

Related Questions