Ashen Malaka
Ashen Malaka

Reputation: 23

How to use websocket in spring boot and react js?

I referred to this spring boot doc to create web socket in spring boot.

I used stompjs and sockjs-client

var socket = new SockJS('/gs-guide-websocket');
//gives an error in below line
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
    setConnected(true);
    console.log('Connected: ' + frame);
    stompClient.subscribe('/topic/greetings', function (greeting) {
        showGreeting(JSON.parse(greeting.body).content);
    });
});

Upvotes: 2

Views: 3825

Answers (1)

Udith Shalinda
Udith Shalinda

Reputation: 487

use this stompjs and socket-client

then import them

import SockJS from 'sockjs-client';
import Stomp from 'stompjs';

to connect websocket

//use your link here
var sock = new SockJS('http://localhost:8102/api/ws');
let stompClient = Stomp.over(sock);
sock.onopen = function() {
  console.log('open');
}
stompClient.connect({}, function (frame) {
   console.log('Connected: ' + frame);
   stompClient.subscribe('/topic/public', function (greeting) {
     console.log(greeting);
     //you can execute any function here
   });
});

Upvotes: 4

Related Questions