Yii2 notification system implementation - email

I am trying to implement simple notification system in Yii2.
I have a list of Records populated in ListView.
I would like to allow users to vote for this records. Only once per Record.
Also the Author of each record must be notified (smth like new incoming emails) about who voted for his Record.
What is the easiest way to implement this in Yii2?
Thanks for an advice!

With php only you can't do that, you need to have js at some point.
Solution One could be, from user B side you keep asking the server after certain time (using ajax), if anything(event) happened. If it does return the result with that ajax and update the page.
But then again this is not a very good practice.
If you really want a Real Time update, you might want to use socket.io. Then again using php socket is a bad idea.
So here comes the Solution Two: yii2 + redis + nodejs + socket
redis will do the communication between yii2 and nodejs. The idea is, if anything (event or update) happens yii2 will publish a event with associate data to redis channel and nodejs which is listening to that channel will catch that event and send the data (or update) to client using socket.
Here is a real time chat application tutorial using yii2+redis+nodejs+socket.

Related

Flutter Best Practices to Continuously Poll for New Data while on a Page which renders that Data?

I have an App written in Flutter, and I wish to have the App immediately render new Objects found in the Database, but the best way I can think of achieving this is firing a GET request to the Backend Database and call "setState()" every 5~10 seconds. This is so that when a user is in his "Order" table in-phone, and places a new "Order" from his/her laptop, I want that "Object" to show up in that users table in phone, without any page navigations or reloading the page.
The Table is implemented as a ListView.Builder()
If you manage also the backend I think one way could be to use websocket. When an order is inserted you call an API that emits an event to a websocket, your Flutter app that uses websocket can listen for this event and eventually rebuild the data.
In this way you don't need to call the GET api every N seconds but the server "calls you".
Here some flutter documentation about websockets: https://flutter.dev/docs/cookbook/networking/web-sockets

MongoDB Concurrency Issue

I have a scenario for my app which is similar to sending friend request in Facebook.
When user A sends friend request to user B, internally a new friend request document is created. At a later time when user B also wants to send friend request to A, system would find out that a friend request document existed and so they should be friend of each other, no new friend request document would be created.
I'm trying to figure out the case when user A and user B both simultaneously sends friend request to each other which will then create 2 friend request documents and leading to undetermined behaviour...
Thanks for your suggestions.. Really appreciated!
Edit:
A few had suggested to use a request queue to solve this; however,
I'm confused about using queue because i thought it would make my rest api endpoint process requests sequentially. Wouldn't I lose all the benefit of multi-threading by using queue? I can't help but imagine how bad it would be if my service has millions of requests queued and waiting to be executed one by one just due to this issue. Has anyone seen something along similar problems seen in production?
I had similar situation with my client which has concurrent writes in the database, What I have implemented is a Queue service.
Create a request in the queue rather than writing in the database, a separate reader will
read one message from the queue at a time and check if it is valid to write it to
database, write only if there is no previous request.
You can implement your own queue or you can use service like AWS-SQS, rabbitmq, MSMQ etc.
// Specific to your case
In mongodb write operations on a single document are atomic.
mongodb has a feature of unique index.
Hence if you insert the document with an _id(or any other unique index) with person names A and B by creating unique index for both (such as "A_B" by lexicographically sorting the names) before doing insertion. You will inherently be able to insert only one instance of that document.
// General
What essentially we would like to have are transactions but since mongodb doesn't support such, as of now. There are a few tricks to achieve this:
2 phase commits :
https://docs.mongodb.org/v3.0/tutorial/perform-two-phase-commits/
Using an external source to maintain a flag, for example using memcache which supports insertion in transactional manner/Compare and Swap.
Here if you use system calls method in frontend then you should fire one request to frontend from Database when some user like, I send you request then within a sec database send you one system call and your frontend code immediate correct the button text like
"Add a friend" to "incoming request"
or else.
if you are only setting up database then just make a system call which send it to UI when friend request arrives or as you say Document created, the further process will be handled by UI Developer.
Thank you.
if you don't like the answer then I m apologize for that but don't downvote me because I M new in Stack Overflow Community.

Laravel socket chat application

I'm new with web sockets, and i want to create a private chat with laravel between authenticated users and anonyme users , i'm not asking to give me the codes, i want a way to do that , i want to understand how can i do that
I'm thinking to that for a couples days ago, and i fount that i should make this steps :
1- Create chat with socket
2- intergrate it with laravel (1)
3- show connected users ( i don't know id if that i should make that with socket or with laravel framework )
4- fix the chat to make it private ( build some socket or somethink like that)
i want to know how i can do that ?
Thakns
https://packagist.org/search/?q=socket
If I were doing this, I would separate the sockets server and the frontend implementation.
This would allow you to scale both the dispatch and the client services at different rates.
You can use any library of your choosing, usually the best is going to be on the top.
http://www.sanwebe.com/2013/05/chat-using-websocket-php-socket
That explains it a bit more than I am willing to.
basically you will have one instance of laravel that is only responsible for rest/socket communication. It will provide the client(frontend) with the information that it needs.
When a registered user logs in, You simply notify your socket server via rest a user has logged in, In turn your socket server will query the db for all currently logged in users, trigger the event of UserLoggedIn , attaching perhaps an array of logged in users which would then be broadcasted to all of the listening clients.
When a client receives that notification, your js (if necessary) would update the list of available chatters with the one provided by the dispatch.
You will also need to maintain a list of active unregistered user socket connections so that you know who's where and who should get what message.
This is the general idea behind it.

Meteor : How to implement a private message in a chat room?

Actually I'm more interested in the techniques to use to achieve this task more than really building a chat system (which is an excellent concrete example). I see 2 parts:
The client needs to get registered somewhere, and we then need a unique ID per client.
The server should be able to send something to the client only from another client.
For the first part, I do not know how to get this unique id. Possibly using the new meteor auth kit ?
For the second part, I thought about building a per-client collection in which one and only one client will have access to, but it sounds heavy and In my opinion not really in the Meteor best practice. I then thought of adding a "from" and to "field" to a Message (see the regular chat example). This would do it but I'm wondering about the no privacy on them. Would a custom publish returning a filtered find do it or it is risky too i.e. would other client get the items too ? Something like:
Meteor.publish("message", function (clientID) {
return Messages.find({"dest":clientID }, {});
});
The latest Meteor todos example uses the new auth system to identify private todo entries. I would imagine that you could use the same mechanism to identify the originator and recipient of a private message in a chat like system.
Of course the filtering of which messages someone sees would need to be filtered on the server side to maintain privacy.

agsxmpp event order

Have a problem with agsXMPP. What I want to do, is connect to openfire server as "User1" (as example), check which users in my roster are online and return collection of ONLINE usernames.
The problem is, that OnPresence event is the last event (based on my research), and it occures for each online user. But how can I know when it has finished checking all the online users, so I could finally return my ONLINE collection?
Is there something like OnPresenceEnd event?
Please help!
no there is nothing like that. The xmpp server sends every presence in its own stanza. And there is nothing in the XMPP protocol which would tell you that all initial presences are received now.
XMPP is an asynchronous protocol. When possible keep your code also asynchronous and update the presence after each OnPresence event.
xcc.OnRosterItem += new agsXMPP.XmppClientConnection.RosterHandler(method_name1);
xcc.OnRosterEnd += new ObjectHandler( method_name2); <---This is what you need