Websocket push messages to a specific user with Spring Boot - STOMP - Part 3
Vložit
- čas přidán 14. 08. 2021
- Short tutorial on how to use websocket to push some STOMP messages on specified topics to the frontend using Spring Boot. Same approach can be used for push notifications.
In this video we take a look how to send messages to a specific user.
Spring Initializr: start.spring.io/
Source code: github.com/liliumbosniacum/we...
Intro by Audionautix is licensed under a Creative Commons Attribution 4.0 licence. creativecommons.org/licenses/
Artist: audionautix.com/
It is the best explanation I have ever seen about websockets. Thank you bro, you are the best!!!
Clear and to-the-point! Best video on websockets I've seen! Thank you so much for keeping it short and simple while communicating the relevant info.
I've been stuck with send private and this video is life-saver.
The best video about websockets which i have found ❤
I really want to say thank you for these videos. You do a really great job
Hvala Bogu jedan normalan tutorial koji na vrlo jednostavan nacin ovo objasnjava. Na netu ima more stvari na ovu temu. Dosta njih je potpuno pogresno, a neki ga komplikuju do besvesti. Hvala puno!
Huge thanks bro. You helped a lot 😊
Happy to help
GREAT VIDEO!!!
Thank you for this video.. Helped a lot:-)
Glad to hear that!
THANK YOU VERY MUCH
I can't thank you enough for this. I have been trying to get this to work on and off for a couple of years. Turns out what I was missing was this UserHandshakeHandler class. In its determineUser method, I can see that it receives the following arguments: request, wsHandler, attributes. How would an angular/react/etc front end include a jwt token in one of these arguments in order to determine the user?
Sorry for the late answer. I was away for some time. I think you could integrate is in the message somehow. Maybe this would help you out czcams.com/video/-3pZ9doFdGM/video.html
Thank you so much for this tutorial series. Is it possible for you to create tutorial for an android app to communicate with a spring server?
thanks!
is there a way to send to group of users ? ( but not all)
Hi, thank you for this clear tutorial. I have only a question: if the user is logged in many browser window (or devices) at same time, does the "send message to user" will send the message to all user sessions at same time? (Maybe yes, I think)
Yes, correct
Thanks for your tutorial. What about a scenario where you have multiple instances of this service receiving requests? How do you handle this since the current implementation use in-memory topics?
You could put a lock around your code. There is a tutorial about redis locks on the channel.
You can use the listen and notify of postgres too, depending on your case
Nice tutor. Subscribed.
But how to handle case, when I need to send message to all users, exept the one specific?
You can see that in the previous two video in this playlist. Exactly that case if covered. You can find the playlist on my channel.
@@liliumbosniacumcode thank you for your perfect videos waitin more..., how could i find it (the playlist answering our question) ?
Thanks a lot. Key was to create your own UserPrincipal. Is it possible to keep socket sessionId in Principal name instead of UUID?
I guess so, should be possible.
is the /user part being automatically handled on server side? Because I don't see it anywhere configured. Or am I missing something?
I had problem with it, /user was not seen. But when I changed annotation @SendTo into @SentToUser it's working
hi hello thank you for your effort please i have a question how to use UserHandshakeHandler with the database i meant for example in my project using spring security so i want when the user logged in store a session by username of the user and not randomId thank you for advance
I have the same doubt
Do let me know if you get any solution
@@nanditpujara7073 sure bro
I´m here for the same, did you found a solution?
Thank for this video. I have a query again… you have taken us from tomcat console to send it to particular user… how in our program, how can we get the id of a user ?
If you mean how to do it in the backend, you can do it via spring security service.
@@liliumbosniacumcode could you please elaborate or share any useful link? thank you!
@@swapnilhadge7383 spring.io/guides/gs/securing-web/
With this then you can fetch current user and get its ID
Hey, I have a question: is it ok to send private messages using @PostMapping method? Isn't it an http request which we should avoid doing and instead use websockets entirely?
Sending private messages using the `@PostMapping` method in a traditional HTTP-based application is a common approach, and it's perfectly acceptable for many use cases. You don't necessarily have to use websockets for private messaging, although websockets can be a more efficient choice for real-time communication.
Here are some considerations:
1. HTTP POST: When you use `@PostMapping`, you are making an HTTP POST request to the server. This is a stateless request, meaning that the server does not keep a continuous connection open to the client. It sends a request, processes it, and sends a response.
2. Websockets: Websockets are a different communication protocol that allows for full-duplex, two-way communication between the client and the server over a single, long-lived connection. They are well-suited for real-time applications, such as chat, where low-latency communication is required.
3. Use Case: The choice between HTTP and websockets depends on your use case. If your application requires real-time, low-latency communication, such as a chat application, online gaming, or collaborative editing, websockets are a better choice. However, if your application involves occasional private messages, an HTTP POST request can work fine.
4. Complexity: Implementing websockets can be more complex than using traditional HTTP requests, and it may require additional infrastructure. You might need to consider factors like scalability, load balancing, and connection management when using websockets.
5. Security: Security considerations differ between the two methods. With websockets, you should take into account security measures like authentication and authorization, while with HTTP, you can leverage standard security mechanisms like HTTPS.
6. Compatibility: Websockets might not be supported in all environments and by all clients. Traditional HTTP POST requests are more widely supported.
7. Resource Usage: Websockets tend to be more efficient in terms of resource usage and can reduce unnecessary overhead for applications with frequent real-time communication.
In summary, it's okay to use `@PostMapping` for private messages, and it can work well for many use cases. However, if your application requires real-time, low-latency communication, websockets may be a more suitable choice. Ultimately, the decision should be based on your specific requirements and constraints.
@@liliumbosniacumcode Thanks for such a huge explanation. I have figured out the way to send private messages to the specific users, now trying to figure out the way to authorize the user when logging in, and pass that session info to the backend to get all the chats from DB.
cool
May I ask? To send message to a specific user I can use /secure/queue, but I don't found any tutorial about this, and all guiders use /topic, but it doesn't secure. Can You help me understand?
If you are interested in security you can have a look here czcams.com/video/-3pZ9doFdGM/video.html
@@liliumbosniacumcode Thank you
what to do for this memory leak error
2023-08-03 18:07:23.163 WARN 17872 --- [ Thread-197] o.a.c.loader.WebappClassLoaderBase : The web application [ROOT] appears to have started a thread named [MessageBroker-4] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
Hi! I have a question. I want to create a simple one on one chat app. But the problem is, I want to pass the username from the frontend to the backend, in order to log 2 users with the same username (because that username will be the conversation name actually). The 2 users will subscribe to the same topic with the same username so both sides of the conversation will get the messages. How do I do that?
Hi. Thank you for your question. You do not need to sub both on same topic, but more like user1 subs to user2 topic and user2 subs to user1 topic. Boths users will still see messages they send without subing to their own topic. Hopefully this helps you out.
@@liliumbosniacumcode ok but how the user 2 get the randomID of user 1 ? handshake cant have header so even if the user is authenticate frontend cant have the id or token so in handshake how do you register a user with something that can be shared or stored to be shared after.
@@jaipasletemps222 In a traditional STOMP-based messaging system, multiple users can subscribe to the same topic, and they will receive messages sent to that topic. This is a common pattern for creating chat applications where multiple users can join a chat room or channel and exchange messages.
In your case, where you want to implement a one-to-one chat application where two users can join with the same chat name, both users can subscribe to the same topic to receive each other's messages. Here's how it works:
- User A and User B join a chat with the same chat name (topic name).
- Both User A and User B subscribe to the topic with that chat name.
- When User A sends a message, it is sent to the topic with the chat name.
- Since both User A and User B are subscribed to that topic, both users will receive the message, effectively creating a one-to-one chat.
This approach allows for real-time communication between the two users by using a shared topic, which acts as a communication channel for their conversation. The messages are broadcast to all subscribers of that topic, which, in this case, are just the two users involved in the chat.
Are you from Russia?) I think so, because your English has a Russian accent.
No, I am from Bosnia :)