Laravel SSE Real-Time Notifications on Shared Hosting! No WebSockets, No Pusher, No Echo!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] Hello friends welcome back to the channel in this video we are going to learn how we can send Real Time notification and lar in a shared hosting where we don't have an access to web sockets so as you guys can see in here I have two browsers but I'm in the share hosting I uploaded my files so now I want to send a notification using this browser to this browser in here I'm admin and in this browser I'm user so if I go and click on Real Time notifications now in here I will select the users so for example I want to send to the user one something and uh I will say here subscribe to the channnel send now after a few seconds as you guys can see I got a notification in here that says subscribe to the channel I can send as many notifications as I want to any user which I have so let's see how we can build this so let's start by creating our notifications table so I will type PHP artisan make model notifications and with the migration and controller and the let's now let's go to migrations database migrations notifications table in here I will type my column name so I will pause the video okay so I wrote the I added one column for message to store another column for user ID the one that we want to send the the notification and one column to check if this notification is sent to that user or not now let's go and create our view first of all in the web.php in here I will type route gate send notification and I want to go to notification controller so notifications controller class and insight index because I want to send the user ID as well so that's why I'm creating the index otherwise I would have choose view route so public function and index and in here I will create a variable or to store the users I want to get all the users so users all and I want to return view SS I will create this now and with that user variable so compact users let's make this also users ruler okay so let's create our view let's directly create here ss. play. PHP okay in here I will I will create a select option input and a button to send a notification so I will pause and create create then I will be back okay so I created the the page in here I just look through all the users to get all the users with the ID and the name and one in text box to write a message and one button to send a not notification and I'm sending the notification using ax so I created the aex as well if if I come scroll as you guys can see a I'm just sending the csrf token the ID and the message now if I come to my browser refresh as you guys can see I have the users and the input box and the button now let's go and create our controller as well so first of I have to create this route so I will copy this come to my web.php and in here I will type route post create same thing notifications controller class create now let's go and in here public function create accept request doson request Dolson item is equal to new notifications T message is equal to request what we get from the request the message and there's an item user ID is equal to dollar sign request D ID at the end item Das save man one more thing return this paon Jason add it successfully now if I come to my browser refresh the page and write something here please subscribe to the channel and click the S now let's go to our database refresh our database as you guys can see it save so our notification part is done now we are going to create our first SS or server sent event so uh our application should be able to send the client the notification so let's go and create uh to our project in here let's create our One controller by typing PHP Artisan me controller and give it a name of ss e controller enter it's done now inside our SS controller close this first of all I will fit all the notifications so I will type notification is equal to notification where user ID is equal to all user and ID and another conation where is send is zero which means it's not been sent yet now the first one get the first one uh we forgot to create our method so let's create our method by typing public function send SS and cut this from here P it in here okay after this we have to set the headers for our server send event so we have to type header and and inside this header I will type content Das type is equal to Text slash event and stream then heer cach control no cachy one more time hi connection and keep alive so this is the header of our event that we will send with our events and after this in here I have to check that if notification is available then send the notifications otherwise we have to just send an heartbeat to keep the connection alive if there is nothing we don't send anything from our server site to the client site the connection will be lost and we have to reconnect the connection either by refreshing the page or creating a new method so it's better to keep the connection Alive by sending some hard bit or an empty string so first of all Let's uh type here if notification is available on that case we will send an create a variable and and inside that I will send my message which which should would be notifications message now we will send the even data so for that I will type Eco data and after that I will in incode my data so I will type Json underscore incode and my do event data Dot slash n slash n then after that it send I want to update the notification to make it sent is equal to one which means that it's sent so we should not take it again so let's say notification is send is equal to one and notifications update now let's create our else part so if notification is available so we will send our data but if it's not available we don't want to lose our connection we want the connection to be connected so in that case I will type EO just send this new line as an to keep that connection alive and at the end I will type OB uncore Flash then Flash so starting from here up to this this line and this Line This is How we can create our SS and larav the rest is just uh checking the notifications and updating the notifications so only this part this is 1 2 3 4 five six lines uh we need to type now we also have to create JavaScript function to to get this event so for that I will go to my layout page I want to be in the top so every page can access it doesn't matter in which you page the user is it it should display the notifications so in here in my layouts app. blade. PHP after the body I will create a script and I will create a variable with the name of source and set it to new event source and I will type my URL in here so let type URL SS update so I will create this URL later so let's uh actually create this one now let's uh let's type it out colon colon gate this is update and same thing that we method that we created in our s controller send s we want to link it to this so in here I will type SS controller colon colon class and my method that same method that we use to send the event send S and S controller so this part is done our notification is finished now let's go back to our app app. plate. PHP now let's uh display our message so in here I will type Source do on message is equal to function event and create a variable for example HC just a variable name now we have if you guys remember in my S controller in here I encoded my data so I have to back decode my message so for that and I will type json. par even. data now we just have to show it in the notification so I'm using notify DJs so I will just use that in here as well so I will type dollar sign do notify and AC do that message that I get and it should be success now let's go to browser and test this okay so if I come refresh my page and create a message in here for example hi send now after a few seconds yeah I got the notification now let's go and open a new so in here let's open a new window and let's log in so we should be able to send to multiple users so user one at user.com and the password okay it's log in now I will go to real time notification and for the admin I will say hello world now let's send the notification let's open this as you guys can see it I got hello in here and if I type here hello again send here let's see yeah I got the notification so this is how we can create a real time notification system and a shared hosting without using any web sockets hope you guys enjoy the video and if you have any questions write down in the comments and don't forget to press the Subscribe button
Info
Channel: Stack Tips
Views: 1,093
Rating: undefined out of 5
Keywords: Laravel SSE, Real-Time Notifications, Shared Hosting, Laravel Tutorial, PHP Web Development, No WebSockets, No Pusher, No Echo, Laravel Event Broadcasting, Laravel Push Notifications, Laravel WebSockets, Real-time communication, WebSocket technology, Laravel package, Laravel development, Bi-directional communication, WebSocket server, Broadcasting events, Real-time data updates, Chat applications, Live notifications, Collaborative features, laravel
Id: A7I8r3Fhrww
Channel Id: undefined
Length: 15min 43sec (943 seconds)
Published: Tue Jan 23 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.