How to add a dynamic results section to Laravel - 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

Related

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.

Use Nchan to post to logged in users only

I am setting up an application with realtime events using nchan (https://github.com/slact/nchan). The application itself is built on the Laravel framework. However, for now everyone that subscripes to specific sub-channel will get new events as soon infomation is pushed to the corresponing pub-channel.
I want to limit that so only logged in users should be able to subscribe to events. Hence, if I am not logged in, it should not be possible to create a websocket and listen for events from for example http://www.example.com/chat/pubchat?id=some_id.
Is there any way to make this possible?
As far as I know, I cannot use Nginx/Nchan to make database queries to get only logged on users that way. And Laravel itself cannot limit subscribers since that is handled by Nginx/Nchan (but on the other side, can make database queries).
Thanks for any tips!
EDIT: Seems that I might can use the Drizzle module (https://github.com/openresty/drizzle-nginx-module) and use the session ID as identifier. So if that ID is not present in the session table the connection will be blocked.

Creating a Slackbot that adds

Hey in my team's slack (messaging system for those who don't know) we have an automatic response, so that when anyone says "trump", slackbot automatically responds with "the wall just got ten feet higher". Now I want to make a counter that essentially allows slackbot to state "the wall just got ten feet higher, wall height:(have a updated value according to number of times "trump" has been stated)" So basically I want a way to have a value that updates the wall height but I am lost on how to do that within slackbot. Any help is much appreciated, thanks to all!
The default features provided by Slackbot only allows it to respond to keywords, but not much more. So to provide that additional feature you would need to develop a custom bot.
For your use case I would recommend building a so called internal integration for Slack using the Events API.
Internal integration allows you to add custom functions for your Slack team only (as opposed to a full fledged Slack app, that could also be installed and used for other Slack teams).
The Events API allows you to set up a bot that listens to messages and can react to keywords like "trump".
An alternative approach to the events API would be the outgoing webhook. However this function is now deprecated and should no longer be used. Also it only works with public channels.
To set this up you will need to develop a small webservice (e.g. in PHP) that listens on a webserver for requests from the events API, keeps count of how many times the keyword has been invoked in the past and sends an appropriate message back to your Slack team every time the keyword is used.
I can recommend reading the excellent official Slack API documentation if you want to learn more.
If you are familiar with PHP this can be done easily using the Slackbot Framework. It supports Events API allowing you to listen to messages in channels or direct messages (depending on the permission scopes of your APP). So all the conversations on Slack can be sent to your server and you can search for the specific keyword in every message. Then send back an appropriate message to Slack. In summary, the first step is to create an APP for your slack team at https://api.slack.com/apps?new_app=1. Next step is to install the Slackbot Framework which is explained here. Hope this is helpful.
That can also be done by integrating custom slack bot using Django. You'll have to subscribe events and based on events, Slack will send conversation message to the given url, and based on the event, you can write your logic to increase count and post message back to slack work space.

How to implement a chat application in codeigniter website?

I need to implement a chat window on my codeigniter website,and the chat option is not like facebook or gmail chat.The Live conversation is entirely between the admin and the user who has logged into the site,ie live chat for direct customer service
Thanks in advance.
A quick search on github gives me this:
https://github.com/Runnable/Chat-Example-App-for-Codeigniter-API-on-PHP
https://github.com/vgoodvin/ci-chat
https://github.com/llbbl/codeigniter-chat
You can think of it like a thread in a forum where someone creating a thread and the other someone make replies.
Depending on how you want to customize your apps, the interaction between the OP and those who will make replies will be rapid.
EDIT 1:
Assuming that you understand how MVC works, you can have something like this:
Your DB Structure:
Table User
Table Session
Table Message
A user can be in many Session (chat room), A message can only be posted to one chat session and a user can send many messages.
In your chat page you will have a to display the conversation, a giant text box where your user can write their message and a submit button.
When a user click the submit button, it will then make an HTTP POST to your controller, where your controller will cleanse the data (i.e. $this->form_validation->set_rules();
If the posted data is valid, send it to your model where it then be stored to your database.
Everytime the chat page is loaded what you want to do is:
Get the chat message for the associated session ID.
Again this is an oversimplified example. You can fork the code from the github i mentioned and try to install it on your local machine.
If you want messaging or chat and specially for codeigniter than use this library, Mahana-Messaging-library-for-CodeIgniter. I have used this, and i preferred you if you are learner. It have it's on database which you can manage and it will integrate in your project easily.
Implemented using Zopim chat widget,which is suitable for both CMS and MVC websites. https://www.zopim.com/

MVC: Where to trigger user registration emails

I am building an MVC application (using the Zend Framework).
When users first register, the applicaiton sends them an email. My question is, where should I trigger this email from? The model or the controller? My thoughts are as follows:
In some ways, the model makes sense, since sending a registration email is part of my business logic. Users must click the link in the mail to validate their email address.
But by putting it in the model, I am 'encumbering' the model. The model's registerUser action is then only useful within the context of an application that needs emails sent for every registration.
Instead, by triggering the email from within the controller, my controller would be a litter 'fatter', but my model a little more 'fine grained'.
I have written an email service which actually configures and sends the email, and I think this is a good design decision. I am really just asking where I should be calling this service from.
Your thoughts are most appreciated!
According to Zend Framework's definition of MVC, you should put send the email from the controller:
Controllers...decide which view to display based on the user's request.
Models, on the other hand, contain:
...basic functionality behind a set of abstractions.
An email may be considered a "view" in that it displays information to the user. It is the controller's job to activate this "view."
In my opinion, I would want this in the model, as I would consider this an assumed process of the create user method, rather than any specific interaction with the user making the request.
In other words, since I would always want this email sent, regardless of the source of the request, I would identify this as a natural byproduct of the create user action, similar to a record being saved in a database.
You might want to look into using something like NServiceBus to queue messages to be sent to your Email Service.
This way you can have NServiceBus subscribe to an event that occurs and omit any manual firing of the email service etc.
Ultimately you want a failsafe way of ensuring your messages get to the intended people. This kind of framework will greatly help you ensure that this happens.
Alternatively you could store the emails to be sent inside your database and have your email service check the database queue every x minutes for new emails to send and omit the need for triggering the email sending.
Again, doing it this way will ensure at the least that the emails get sent. Should the network go down or some other disruption occur during the sending of each email you can simply leave them in the queue until the network comes back up.

Resources