How to Send Push Notifications with Spring Boot Using Websockets and STOMP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey folks and welcome back and in today's video we'll be looking into how we can send push notifications using springboot application now to do this we'll be using websockets and then we will use the storm protocol to send messages to the various clients so first we will look at how we can send notifications to all the clients and then we will look into how we can send notification to specific clients so with this let's get started okay let's start by going to start.spring.io and here what we are going to do is we are going to add the dependency for web sockets so we are going to add websocket dependency here and then we are going to give it an group id and an application name so this is the only dependency that you need for now so you can already generate this particular project now i already have created this particular project and i have filled in with some code so let's look at the first configuration that we will have to do so if you see here i have this particular configuration here that is for the broker configuration and the stomp endpoint configuration now here what i'm specifying here is i'm registering a slash ws endpoint on which i will be doing the socket communication so basically the web socket communication right and then after that i'm also specifying that now in case if the websocket connection does not work because some of the browsers don't support web sockets then in that case it can fall back to the soft.js implementation so this i'm providing it to the same ws endpoint this is the stomp end point through which actually the client will connect to the application itself so now the next thing that we will look into is setting up the broker itself so we are setting up an internal broker which will actually hold the messages and replay it to the various clients so for this what we are doing is we are setting up two destinations we are setting up the all destination and we are setting the specific destination now the intention of the all destination here is actually to broadcast to all the clients that have been connected to this particular application while the specific one is going to be used to send notification only to specific users so these are the two destinations that we have set in here now the third part that is here that is this application destination prefixes so here what i'm setting it as flash app now what is this particular destination prefix so this is basically like any message that has been sent to this particular prefix is going to be ended up to a particular controller now where is this particular controller so we have this particular message controller which defines here that first of all any message that comes in with slash application we are going to handle it internally what does this mean is that any client that sends a message to slash app slash application would be handled here so this is implicitly inferred from the part that we have defined in the broker settings here so it is slash app and then after that you have slash application so now what this thing does is a client that sends a message on this particular or a notification on this particular destination it would then route it back to all slash messages that means all the clients that have subscribed to this particular destination will start receiving this particular notification now regarding the private endpoint we will look at it a little bit later so now let's look at a client application now this client application is pretty simple so what have i done here is i am creating a particular websocket communication here and then i'm creating a storm client which will run over the particular web socket here what i do i connect and then i subscribe to all the notifications that come on this slash all slash messages and then nothing i'm just showing this particular message so what this show method does is it actually just puts this particular message text into a p tag and then afterwards appends it into this response so if you check here we have this particular messages section in which it will put in the particular text pretty simple ui what this ui has is it just has a button called as send message and it has a text field which has some particular text now the idea here is whenever i click on the send message button it actually triggers sending of the message to slash app slash application so basically i'm trying to send messages to all the connected clients so this would then be handled by the controller here which will then send the messages to all the connected clients which would then be read by all the subscribers here now with this let's actually start this particular application okay so now application has started now what i'm going to do is i'm going to open a new tab here and i'm going to go to localhost 8080 now i have opened two tabs here in both the tabs i'm going to actually open localhost 8080 now these are the two clients that have been connected to my application right so here what i have i have the send button through which i'll be sending a notification to all my connected clients so what i'm going to do here is i'm going to put in some text like notification to all when i press the send button this notification gets sent to all the connected clients so as you can see here this client which was also connected it's just subscribing to all slash messages is also getting this particular message right now this is how you can actually broadcast messages or notifications to all your connected clients now there is a situation wherein you want to actually send messages to a private user right for this what we need to do is first of all we need to have the user logged in because only when the user is logged in that's the time you know that you can actually send it to the right destination as such right so for this what we are going to do is let's go to the pom file and we are going to first of all add spring security now why are we adding spring security here is so that we want a particular logged in user as such so what i do next is i go to the security config so in the security config i first of all configure the security such that it actually allows all connections to go to the slash ws which is a socket endpoint itself right so i don't want any authentication to be done on the circuit endpoint but any other request it needs to be authenticated and i'm then setting uh out of the box available login from and then i'm also setting a logout handler url now what i'm doing is i'm also setting an inbuilt user called as test and test with a role user here now this is just security config that i'm trying to create here i don't want to register a new user completely and then afterwards show you the entire process but rather have a user already which i know is present here this is the security related config right now the next thing is that we need to actually handle the message such that it has to be sent only to the user that i'm trying to send it for this reason what we have here is in the message controller i have this special path called as private now in this part how this incoming message will come is any client that sends a notification on slash app slash private will be handled by this particular function here now the message that comes in has two parameters it has basically the message itself and it has the two part here the two is basically the message that i want to send to this is nothing but it's a custom class that i've created here it has just a text and a to field this is the part wherein i have handled the controller let's look at the client code now in the client code what i'm doing here is so here we have a private storm client which is again going to set up a communication over the websocket and then what it does is it then subscribe to this particular topic called as user slash specific now you must be wondering that why is it user slash specific and where have we specified is user slash specific and to answer this particular question whenever a user logs in and he subscribes to something called as user slash specific spring automatically because a request has been sent from an authenticated client so spring automatically knows that this particular client has to be communicated with the path user specific hyphen the session id where does the session id come from the session id is basically the current user's login session id since this request is right now being sent from an authenticated client this subscribe method is actually going to subscribe to slash user slash specific hyphen the session id let's go back now to the message controller now in the message controller here if you see when i'm sending this particular message which is coming from an external client i'm sending it to specific i'm actually not specifying any session id here or i'm not even specifying slash user this is what this particular template handles since you're using convert and sent to user it takes this particular user it figures out what the session is and it then appends it here internally with it knowing that it has to send it to a user path right so it sends it to slash user specific hyphen the session id and this is how a notification that you get from here is sent to a specific client i have this thing already running so let's actually look at it now here i'm not logged in in any of the windows so first of all let's try sending a private message right so a private notification and i'm going to specify that i want to send it to the test user when i say send nothing actually happens why because none of our clients are actually logged in using the test user so now what i'm going to do is i'm actually going to trigger login here and i'm going to use the test user here i sign in here and now let's test actually sending the notification to all the clients so when i say notification to all so the logged in user also gets it and this client also gets it right but now let's actually try sending it a private message now when i send this private message you see here only the private notification is received by the logged-in user here why because this message was been sent to app slash private which was then routed to user specific hyphen user session in this way this particular client was able to actually read this private notification so this private notification only goes to this logged in user now if i try to log out and now try to send this private notification nothing happens because this session has been logged out let's log in here again and try sending the private notification and the private notification is sent the public notification which is open to all is still open so with this we saw how we can send push notifications using a springboot application to all clients or to specific clients now we used here an embedded broker using which we established websocket communication and then used the storm protocol on top of it now i keep on exploring more such kind of things so if you like this particular video give it a thumbs up and make sure you subscribe to my channel to see more such kind of videos till then take care and see you in my next one [Music]
Info
Channel: Refactor First
Views: 28,132
Rating: undefined out of 5
Keywords: developer, Programming, Coding, Backend Microservices, Microservices Architecture, Software Development, Backend Development, Web Development, Backend Developer, microservices, spring boot, spring framework, how to code with java, how to write code, how to learn coding, spring example, springboot example, spring security, Spring websockets, Send Push notifications, Websockets Stomp, Message broker, spring boot push notification, spring boot messaging, spring boot notification
Id: QNMItUBPxaI
Channel Id: undefined
Length: 11min 37sec (697 seconds)
Published: Thu Aug 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.