Websocket push messages to a specific user with Spring Boot - STOMP - Part 3

Sdílet
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/

Komentáře • 52

  • @andrewhrom3697
    @andrewhrom3697 Před 6 měsíci +3

    It is the best explanation I have ever seen about websockets. Thank you bro, you are the best!!!

  • @chinenyeikpa
    @chinenyeikpa Před 3 měsíci

    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.

  • @lampham7874
    @lampham7874 Před 4 měsíci

    I've been stuck with send private and this video is life-saver.

  • @oskargajewski5904
    @oskargajewski5904 Před 4 měsíci

    The best video about websockets which i have found ❤

  • @edwardsheehan7008
    @edwardsheehan7008 Před 6 měsíci

    I really want to say thank you for these videos. You do a really great job

  • @brianc8005
    @brianc8005 Před 2 lety +2

    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!

  • @ChandanKumar-ou9fr
    @ChandanKumar-ou9fr Před 2 lety

    Huge thanks bro. You helped a lot 😊

  • @Oberhacker
    @Oberhacker Před 2 lety

    GREAT VIDEO!!!

  • @krishnaprasadt
    @krishnaprasadt Před 2 lety

    Thank you for this video.. Helped a lot:-)

  • @huynhtankhoi5412
    @huynhtankhoi5412 Před rokem +1

    THANK YOU VERY MUCH

  • @theo_st
    @theo_st Před 2 lety +2

    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?

    • @liliumbosniacumcode
      @liliumbosniacumcode  Před rokem

      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

  • @DankyGaming
    @DankyGaming Před rokem

    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?

  • @t0khyo
    @t0khyo Před 5 měsíci

    thanks!

  • @haithemnasri7210
    @haithemnasri7210 Před 2 lety

    is there a way to send to group of users ? ( but not all)

  • @Andrea141993
    @Andrea141993 Před rokem

    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)

  • @eduardocosta9078
    @eduardocosta9078 Před 2 lety

    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?

    • @liliumbosniacumcode
      @liliumbosniacumcode  Před 2 lety +1

      You could put a lock around your code. There is a tutorial about redis locks on the channel.

    • @guilhermealvessilveira8938
      @guilhermealvessilveira8938 Před 2 lety

      You can use the listen and notify of postgres too, depending on your case

  • @Nukturn
    @Nukturn Před 2 lety +1

    Nice tutor. Subscribed.
    But how to handle case, when I need to send message to all users, exept the one specific?

    • @liliumbosniacumcode
      @liliumbosniacumcode  Před 2 lety +1

      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.

    • @ibrahimimohssine8131
      @ibrahimimohssine8131 Před 2 lety

      @@liliumbosniacumcode thank you for your perfect videos waitin more..., how could i find it (the playlist answering our question) ?

  • @varunpanwar5577
    @varunpanwar5577 Před 2 lety

    Thanks a lot. Key was to create your own UserPrincipal. Is it possible to keep socket sessionId in Principal name instead of UUID?

  • @Randomisticful
    @Randomisticful Před 2 lety

    is the /user part being automatically handled on server side? Because I don't see it anywhere configured. Or am I missing something?

    • @maciejjaremowicz2646
      @maciejjaremowicz2646 Před rokem

      I had problem with it, /user was not seen. But when I changed annotation @SendTo into @SentToUser it's working

  • @yacinedrissi8811
    @yacinedrissi8811 Před 2 lety +2

    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

  • @swapnilhadge7383
    @swapnilhadge7383 Před 2 lety +1

    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 ?

    • @liliumbosniacumcode
      @liliumbosniacumcode  Před 2 lety

      If you mean how to do it in the backend, you can do it via spring security service.

    • @swapnilhadge7383
      @swapnilhadge7383 Před 2 lety

      @@liliumbosniacumcode could you please elaborate or share any useful link? thank you!

    • @liliumbosniacumcode
      @liliumbosniacumcode  Před 2 lety

      @@swapnilhadge7383 spring.io/guides/gs/securing-web/
      With this then you can fetch current user and get its ID

  • @Poviliuxxxs
    @Poviliuxxxs Před 8 měsíci

    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?

    • @liliumbosniacumcode
      @liliumbosniacumcode  Před 8 měsíci +1

      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.

    • @Poviliuxxxs
      @Poviliuxxxs Před 8 měsíci

      @@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.

  • @nurte4
    @nurte4 Před 2 lety

    cool

  • @sh18rw47
    @sh18rw47 Před rokem

    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?

    • @liliumbosniacumcode
      @liliumbosniacumcode  Před rokem

      If you are interested in security you can have a look here czcams.com/video/-3pZ9doFdGM/video.html

    • @sh18rw47
      @sh18rw47 Před rokem

      @@liliumbosniacumcode Thank you

  • @AlokKumar-ot9sx
    @AlokKumar-ot9sx Před 11 měsíci

    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:

  • @stoiaalin6221
    @stoiaalin6221 Před rokem

    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?

    • @liliumbosniacumcode
      @liliumbosniacumcode  Před rokem

      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.

    • @jaipasletemps222
      @jaipasletemps222 Před 8 měsíci

      ​@@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.

    • @liliumbosniacumcode
      @liliumbosniacumcode  Před 8 měsíci

      @@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.

  • @user-nj5yg9gr8n
    @user-nj5yg9gr8n Před 2 lety

    Are you from Russia?) I think so, because your English has a Russian accent.