Laravel - Checking for a new messages with websocket - laravel

I built my first app in Laravel and I am wondering what is the best way to check for a new messages while you are in conversation with someone? This is how conversation looks now: http://prntscr.com/t976tx .
I want to check for a new message without reloading a page and constantly listening that event.
I was reading about Socket.io and I have no idea how to start with that..i am using Laravel 5.7

Related

Create socket for laravel and vuejs

I wrote the backend with Laravel as an api and the front with vueJs, of course, are completely separate.
Now I want to create notifications for some tasks.
I do not want to use the pusher and I want to create the socket myself (to practice and understand how it works).
Now my question is, do I have to create a separate socket for both Back and Front and connect both of them to the same port?
Thankful.
you can use the laravel-echo on the client-side and use the laravel-echo-server on the server-side.
at the first, you should install and configure the laravel-echo-serve then run it in your server.
now for client-side, install the laravel-echo and listen to your custom port which you defined in laravel-echo-serve config.
And for call your socket, just enough create an event in your laravel project and create an instance like below and place it in your desired location:
event(new App/Events/ExampleEvent());

How to get online users(in real time)

I have a task to display a list of online users present on the site. I've been said that I should use web-sockets for this.
I don't know how to implement this. I've already installed Pusher and Laravel Echo. Could you help me by pointing in right direction? I guess I should listen for the moment when user socket connects.
Reading all the comments I've imagined two possible solutions:
Broadcast when a user logs in / out
This is the very simple solution. You can use laravel notifications that allows you to implement a specific broadcast logic:
During the login / logout phase create a UserLogged[In|Out] notification and broadcast it to a specific channel
Using Laravel Echo and a "few lines" of javascript, update your onlineUsers list (for example in VueJs you may want to update the Vuex store)
But this may require a more complex logic and you may have to write a lot of code to keep the online users list updated... I think it's better to move to solution #2...
Use presence channels
I think this is the best way, since it doesn't require nothing else that a good Laravel-echo configuration. For the example I'm using Pusher (since you asked for), but this solution can be implemented with all the Laravel broadcast drivers.
When a user logs in, simply subscribe him / her to a presence channel (here the Laravel documentation)
Using Laravel Echo, subscribe the user to that channel
var chatRoom = Echo.join('my.chat.room.id');
Once subscribed use themembers.count method to keep an updated list of the channel users.
Using a setTimeout() function you can have a realtime update of the users list.
Notes
I've never implemented this solutions in a production environment, but just wrote some code to see how it works. so I think that if other people have diffent point of view or have a better way to explain this process I will be happy to update my answer.

how to send event to the client after specified time in web socket(pusher) Laravel?

I am beginner in Web Socket. i am building an online exam system. i already have timer coundown system.so while any student taking an exam, JavaScript sends an ajax request every second to the server, so when countdown < 0 then JavaScript reloads the page to stop exam.
but now i want to achieve this functionality with web socket.
how to broadcast an event to the clients when the specified time passed.i am using pusher with laravel.
In Your Exam model add started_at attribute. Then make function which will run every minute (https://laravel.com/docs/5.7/scheduling#introduction) and check if time is expired. If it is just send an event to client using Pusher and reload the browser

How to add a dynamic results section to Laravel

I made a laravel app which uses an index method that delivers a collection of the client's available appointments (think hairdresser) to the page (as an api), which I then display using vue/vuetify.
The client is saying they would like the appointments on the page to be dynamic/live eg if someone books an appointment, then all other logged in users will see that appointment disappear from the list on their screen.
I have no idea how I would do this, although I have had one idea - I somehow incorporate node/non-blocking on the server, like a chat room, but only for this part of the app.
Or is there a way to do this with laravel/nginx?
Thanks in advance - I don't know what to search for!
I believe you are looking for Broadcasting (Documentation Link). You would need to:
Configure your broadcasting driver (you could give pusher a try for quick setup and tinkering)
Configure your Laravel backend to dispatch a new event whenever a new appointment is made, (e.g. event(new AppointmentCreated($appointment)) where AppointmentCreated implements the ShouldBroadcast interface. You can combine this with Model Events
Update your frontend to receive your broadcast (Check Laravel Echo). Once you receive a broadcast, update the UI to mark this appointment as unavailable i.e, make it disappear

How to run in background Laravel 5

Sending email with Laravel takes time before it is successfully sent, or sometimes it even fails. For this reason I would like to store record in database first and running email sending in background to save user's time. After storing the record, I would like to quickly redirect / refresh the page.
How do I use queue in below email sending code?
$message = new Applyonline($post_title, $cand_name);
$message->attachData($pdf->output(), $attach_name);
Mail::to($to_email)->send($message);
Please advise how to achieve this.
You can use Laravel Queues for this use the following link for complete explanation
Queue Thorough Explaination
I suggest looking into queues. Implementing this is quite easy. Just setup a queue for sending the mail and then add the queue to the mail facade. i.e Mail::to()->queue(new MailQueue);
Queues
Queueing mail

Resources