Service Worker: handle requests when offline to send them when online - offline

I have a chat web application and I want it to work offline. For this I use progressive web apps features (Service Workers) to use cache to get the shell app and the messages already loaded.
What I want to do is to be able to make a post message when I'm offline and let the service worker handle the connection issues (i.e.: keep the message somewhere till where are offline and as soon as we are online send the Post message).
I want to use Service Worker because I also want to send the message if the user as left the web app after posting a message with no connection.
What is the best API to use for this?
I saw the background sync API but it is not standard and it doesn't seem to be updated for almost 2 years.
If there is a way to do this in a manner that the client (the web app) is totally unaware of this mechanism it would be cool.
What I mean by that is I would like my app just do a
fetch("/message", {method : "post", body : {content : "hey there"})
And then the Service Worker just intercept the fetch, if we are online then it just send the fetch, but if we are offline it "wait" for the connection to be up again and then send the post.
I wonder if there is an event listener, available in the service worker, that will be activated when the connection change from offline to online. This way I should be able to store the request in indexDB when offline and then send the post when online.
I saw the navigator.onLine but it is not an event :(

Based from this post, you may use a Service Worker in running the app in the background either via its push event handler (triggered via an incoming push message), or via its sync event handler (triggered by an automatic replay of a task that previously failed).
You may check the Offline Storage for Progressive Web Apps documentation for storing data offline:
For URL addressable resources, use the Cache API (part of service workers).
For all other data, use IndexedDB (with a promises wrapper).
You can cache static resources, composing your application shell (JS/CSS/HTML files) using the Cache API and fill in the offline page data from IndexedDB.

Related

How can I make a client server show updated data without send request?

I design a application for a restaurant one of the features is that the user can make an order online and the oreder status is pending until the restaurant cashier or restaurant admin staff convert status by accept or refuse
so i wanna send to client a notification if there is pending orders or something that the admin staff or cashier know that there is an pending order created now without need to update or refresh the client page
so my question is how i make a backend server send to client browser notification without the client need to refresh the page or send request to backend server
i use django rest framework
I hear about websocket and Django channels
Also i hear about SSE
Also i read about client freamwork send requests every n minute to update the page
I need to know what the best approach to implement this and if there is another technology and what is best for server if there is a lot of loading or the application used by millions of users
For example - you can implementig full-state service on nodejs + express + socket.io or asp.net + signalR. Client connecting to your service via websocket if customer open page where there order status. This solution will allow scale only service if connecting many client, also you should think over communication between service and general app on django, for example use rabbit or kafka or other. Also your can use this service for new realtime features

RabbitMQ for http/https request queuing in Django

One of my clients wants to use RabbitMQ for request queuing on his Django DRF powered web app. the app is simple just saving and listing information with attachments (over S3 storage) with high traffic.
As I know RabbitMQ acts as message broker which can be helpful while doing long tasks/sending emails/ generating PDFs.
I am curious to know that in my scenario can I use rabbitMQ? and if yes then what will be the advantages of using it.

Can Hubot Slack bot store sessions

I am trying to implement simple slack bot. So I have configured hubot which will take inputs from slack and passing it to my webapp (django app) and it will take whatever the response from django-app and will reply to slack.
In this process I am trying to store session in django using request.session but that is not reflected in slack. If I am accessing the django-url in browser it is able to store sessions and getting proper response with session.
So does the problem lie with slack or my approach and is there a way to store sessions in hubot when requesting to django-app ??
I can not speak to the specific technologies you use (hubot, django), but I am using server sessions with my Slack apps all the time and can give you a general answer on how it works. Note that my Slack apps are build with PHP, but I think its safe to assume that the principles are the same.
Slack does not support sessions
In general Slack does not support sessions or context. Instead everything is request based. So if you want to have sessions to keep a functional context between requests you need to organize that by yourself in your Slack app.
Challenge for using server sessions with server requests
One challenge is that most server sessions are designed to work with a client that uses a browser. e.g. a PHP server session will store a cookie in the browser, so the server knows, which requests belong to the same session. This does obviously not work with Slack, since all Slack requests are coming from a server and and there is no browser involved.
Approach for using server sessions with Slack
But you can use severs session with Slack with these two tricks:
Manually set the session ID
Usually the ID of a session is chosen automatically by the server, but you can also set it manually. This allows you to tell the server to continue an existing session that was started with a previous request.
Include session ID in Slack control
The functional session of a user is tied together by the Slack controls he uses. (e.g. an interactive button). Its possible to include custom data in those controls (see this answer for details) and that allows you to include the current session ID in it.
Full approach
You include the ID of your current session in the Slack controls, that you create with your app (e.g. an interactive button). Once the user clicks a button Slack will send a request to your app, which will include the session ID. That allows your app to continue an already started server session.

Sending SQL emails using AWS and SES

I hope someone will be able to help me with this since I am new with AWS stuff.
I have a Web App using .NET MVC which will be deployed/hosted in AWS. This is the description of what I would like to achieve:
1- Let's say that the Web App will insert products in a Products Table on SQL Server.
2- When this product is inserted, the system (AWS) will send an email to a Client from a Clients Table on SQL Server.
Is that possible with AWS?
Could I set a trigger in SQL Server and send an email by SES?
Is it better to use SQS?. So the Web App will publish messages in SQS, and then having another app listening and sending those emails, for instance a console app.
I will appreciate any direction or useful link.
Thanks all of you in advance.
The answer to your first question is 'yes'. Yes SQL server can use SES to send emails. Because SQL server can send emails, all you need to do is set it up to use the correct SMTP settings from SES once your account is verified and working with SES.
That said, I would never have my db server send emails, just doesn't seem like the right place to do it; even though you can.
I have developed and support several systems like this, and the usual pattern I use is to have the web application insert a message in an SQS queue that will be used as input to another process to send the email out. When possible I like to include all the details about the email into the SQS message, i.e. from, to, subject and the body - everything the downstream process will need to know to send them out.
In my case I use a windows service running on several EC2 instances in an autoscale group to poll the queue and send the emails out. In most cases, where I was able to store all the emails in the SQS body, the windows service is completely general purpose - it reads an SQS message, composes the email and sends it out. Because all of the details of the email are within the SQS message body, this single SQS queue and the windows service that is processing it, can process emails from a variety of applications because the service doesn't need to contain any business logic specific to the application and has no external dependencies.
As you talk about separation of responsibilities, I can't see anything less indicated to send email than a db server (even if it can do it).Sending emails is a task for your business layer, surely not for the data layer.
Use the web app to trigger the process of sending the emails, than implement it directly into the web app, or separate it with a messaging system (like SNS), with a queue system (like SQS) or whatever else.

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