Home » Java » java – WebSockets on Spring Framework-Exceptionshub

java – WebSockets on Spring Framework-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have lots of questions about websockets and how to use them in Spring.

First of all, I’m building an chat app using Spring framework as my backend, and React as front end. It is working well but I want server to message client, without client asking for it. To do that I need websockets. At the moment I am using Ajax to get data from my REST API built in Java.


Question 1

Question: Can I make requests to the API using a websockets, like I do with Ajax?

Question 1.1

Question: If yes to the question 1, what’s the best way of sending requests? Websockets always connected to the server or Ajax that connects everytime that requests something? What’s the best performance?


Question 2

I’ve read lots of things saying that we need to subscribe to an endpoint and that we receive messages that are sent to endpoints we have subscribed.

Question: How does the websockets architecture work? Isn’t it like Ajax that we make request and server just responds to them?


Question 3

Question: How to handle websockets using Spring framework?


Question 4

Question: How to send messages to a specific user (or session idk)?

About the 4th question I’ve read this posts:

Sending message to a specific connected users using webSocket?

Spring WebSocket @SendToSession: send message to specific session

If that’s the way I need to do, just tell me.

I don’t know if I really understand websockets so that’s why I’m making this post, though I researched. So I would like to have a clear understanding of it and to know how to use them in the context of my problem.

TL;DR – I would like to have some explanation about websockets and how to use them in Spring framework. I would also like to know how to send messages to specific users using websockets.

How to&Answers: