r/javahelp • u/Cazhero • Nov 29 '20
How does the message get received by frontend? [Websockets]
Beginner learning websockets with java (spring boot) with reactjs. And I found this example on how to use websockets and it's like 90% clear on what's happening. The only thing I don't understand is how/where the frontend is receiving the response-message from the backend.
Link: https://www.opencodez.com/java/simple-websocket-application-springboot.htm
I kinda understand something happens here
stompClient.subscribe('/chat/sendMessage', function (greeting) {
showMessageOnPage(JSON.parse(greeting.body).message);
});but does it mean that the showMessageOnPage-function gets triggered automatically whenever it receives a message that gets send to '/chat/sendMessage'?
Extra: I also looked at the documentation for STOMP https://stomp-js.github.io/stomp-websocket/codo/extra/docs-src/Usage.md.html where they use call-back instead of, but it seems that the chat-tutorial doesn't have an if-else.
(I'm gonna post this on a javascript sub because the precise question lies in Javascript, my bad)
2
Nov 29 '20
I don't think your question is JavaScript specific if it's using Websockets.
https://en.wikipedia.org/wiki/WebSocket
A websocket is a persistent connection, it's meant to emulate a socket to socket communication like between two systems.
Basically, they latch onto each other and are continually listening for traffic. As soon as they receive any data, they decode and act on it.
1
u/Cazhero Nov 29 '20
Yeah, but where does the continuous connection lie in the code from the example? I can't seem to figure that out.
3
Nov 29 '20
It's not an easy answer for "where does this live in the code", but the front-end code lives within the stomp library (assuming that's what you're using).
https://github.com/stomp-js/stompjs/blob/develop/src/stomp-handler.ts
This file contains at least some of the framing code for creating and sending messages. It opens up a TCP connection to the backend.
On the backend, you're using the spring-websocket framework, and that code is located here: https://github.com/spring-projects/spring-framework/tree/master/spring-websocket/src/main/java/org/springframework/web/socket/server
Are you trying to find the true source code that's doing the TCP stream manipulation? Or just where in the example these things are defined? In the app, "@EnableWebSocketMessageBroker" is where the backend server is set up, and in the frontend it's just the .subscribe using the library.
1
u/Cazhero Nov 29 '20
Oh...the latter yeah...so the subscribe together with the function provides the continuous automatic connection, right?
1
1
u/dwargo Nov 29 '20
The actual connection is made in the Websocket(url) constructor, which is somewhere in your library.
•
u/AutoModerator Nov 29 '20
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.