Django private messages notification - django

How this real time notification works? Something like notification in Facebook.
I have a webpage in Django. And user A send a message to User B. And this works in my project but user B see the message notification (You have a message) onlny when he refreshes the page.
How to do this in real time? User A send a message, user B has opened webpage and when message has been sent User B automaticaly see that he has new message (without refresh the page).
I can send a ajax request every second from client to server to 'ask' server that does client have a new message. But I think - this is not much efficient and there is a best way to do this.

Related

How to create a bot that automatically responds to comments and sends messages to customers on your Facebook page

I want to develop a chatbot that can trigger when a user comments on a post on the page, then it will automatically reply and send a message to that user.
I'm referencing the following facebook documentation but it doesn't seem right
https://developers.facebook.com/docs/permissions/reference/pages_read_user_content

Django+Twilio: How can I display inside my django app the sms I sent/received using twilio? (Architecture question)

I have a high level architecture question:
I have a django app where users can send out sms to their contacts (telephone numbers, not users of my app). I am trying to implement a system where if someone replies to a specific text, such reply is forwarded to the user that started the thread.
I implemented an inbound webhooks to receive sms but I am now facing a problem: how do I know which text is a reply replying to? I can't seem to find any type of id in the sms inbound request and I am now starting to fear that this is just impossible.
Any idea? Should I try to use "Conversations"? Would that solve my problem and if so, how should I go about it?
Any suggestion?
Twilio developer evangelist here.
You cannot get the message that a user is replying to, because that doesn't exist in SMS. SMS messages are simply chronological. To test for yourself, open your SMS app and try to reply to the second to last message from someone.
You have a few options to try to tie a reply to a specific message:
Only ever send one message to be replied to at a time. Once you receive a reply to one message (or there's a timeout of some sort), send the next one.
Have the user enter a message identifier in their message. You can then parse the identifier out and associate the reply. This is not very user friendly and they may forget or get the wrong identifier in the message
Use multiple numbers to send the messages. So, if you have an active message you are waiting for a reply to, use a different number to send out the next message. You can then associate the reply based on the number the user replies to.
The last option is my preference since it doesn't affect the messages you can send at any time and doesn't require extra work on behalf of the user. It does require extra work to build a number pool and do the work to associate messages with sending numbers.

Deal with timeouts when posting data -no ajax

The use case:
User makes order his payment gets accepted and his details are getting post to a django's view. Using these details django's view creates user and everything that is necessary (Username and password is provided by me). Then before returning it sends email to clients email with his data (Username and password for now).
But sometimes I get a gateway timeout error from apache(app is deployed on openshift). Because the user is created I assume that the timeout comes from the email sending part. How can I make sure everything went ok and inform the user? How can I make sure that if the email isn't sent I can resend it? What is the best practice at that?
If you have timeouts with an API or Service, you should fire your POST / sendmail request with AJAX...
Serialize the whole form (like jQuery's serialize())
Send that data via AJAX (with jQuery's ajax())
Inform the User of success or error (alert() or jQuery UI dialog)
You can find a lot of examples on this website.
Another "dirty" approach would be to add the attribute target="_blank" to your form tag what opens your lazy request in a new tab / window.

implementing stackoverflow's notification system with django

I've seen questions like Notify panel similar to stackoverflow's. It talks about the client side of the implementation.
I'm looking for the information about the server part and the networking part (how client get notified real time)
A user scenario might look like this:
something happens for user-a
server creates a message for user-a in DB (for persistance) : I'm using django-activity-stream for this
server sends (new or last 10) messages to user-a's browser (when user-a logs in or when event happens)
browser displays the message (Notify panel similar to stackoverflow's part)
if user acknowledges the message(clicking the inbox in SO), all the unseen messages are marked as read and recorded in server
I have questions on the following steps.
(3) Not sure but https://github.com/stephenmcd/django-socketio could be used.
(4) The answer to the question says client has the json data received from server.
Does server send messages to user for every request?
Does client check local storage(I'm new-to-web, what's a good local storage for this purpose?) and request the json data if he doesn't have them in the local storage?
(5) How should I implement this seen and unseen? django-activity-stream doesn't have notion of them.
This can easily be implemented by using django-channels.Because you need websockets to have a two way client server communication.
Showing notifications is a two way communication. Server notifies the client that a new notification available. The client shows this notification to the user, and then when a user interacts with the notification, the client notifies the server that notification was read, so the next time user loads a page, only unread notifications are shown.
There are some steps involved.
Your server needs to be able to support websocket communication. django-channel converts the application to ASGI.
Create a websocket consumer that can send and receive messages to a websocket.
When user opens the application, the client creates a websocket connection channel to the server.
Whenever a new notification needs to be sent, the server will send the message to the channel.
On receiving the message, the client renders the notification on the webpage using Javascript. Like showing the new message icon, appending the new message to the list of messages, etc.
Now, one part is done. Your user has been notified. Coming to the second part.
User sees the bell icon or whatever, and click on it, he sees the notification details (this was rendered by the js, when client received a message).
User clicks on the notification/bell icon. At this time, the client will send a notification back to the server, so that server can update what all notifications were read.
I created an app that updates the client when a new message is to be shown. Github link.
You can also refer to a similar question: https://stackoverflow.com/a/55656848/4186008

Django block request

I have a problem with my website. there it is :
No problem in this case :
User A do a request.
Django server update data...
Object 1 is updated.
Object 2 is updated.
Object 3 is updated.
Django render the result.
User A do a request.
Problem apear in this case :
User A do a request.
Django server update data...
Object 1 is updated.
Object 2 is updated.
User A do another request !
User A do another request !
Django render the result.
the problem is that if the user makes a request while the server has not finished working, the server stop the process and data are no longer good.
I think i have to block client side (by displaying a loading message for exemple) but i want to be sur the user cant make a request on the server if another treatemment still working.
thanks for your time.
This sounds like you have a long running process, and the user gets tired of waiting, clicks on "stop" in the browser; the other scenario is - user opens another tab and sends the same request again.
A few ways to solve this problem.
Use the transaction middleware to prevent your db objects from being corrupted if the request is interrupted.
Use a task queue like celery. At the first request, offload your task to the queue/broker. Send the user a message that their request has been accepted for processing. Now the user is free to send a duplicate request. If they do, you can check what is the status of the queue and reject the duplicate requests.