What is the difference between server side event and Web hook [Reverse API]? - server-side

What is the difference between Server side events SSE and Web hook?

The difference is:
1) using SSE you pull updates to your Front-End.
2) using Web-hook you pull updates to your Back-End.
The first is simplier, easier to implement and manage.
The second is more secure/reliable.
Also, some server may implement long-polling, which will delay response until new data arrives, so it might be used instead of SSE.

Related

How do i bind a react component to the database state?

I want to bind the state of a react component to a backend server running on Django. What i am trying to achieve is an ajax call that constantly updates the state of the component, however, constantly making an api call after every couple of seconds might have an impact on the performance of the application and hence, i am looking for a better way to achieve the same.
You've got two possible solutions -- polling and websockets. Polling means making a call at a specified interval asking what's changed. That's easy, but as you said, can be inefficient. Websockets involve the server sending push notifications to the browser saying when something changes.
This site gives an introduction on how to use websockets for what you want. It's better, but more complicated to implement.
You'll have to make a judgment call about traffic -- if your traffic could handle a polling call every five seconds from every user, that's the much easier option. If not, websockets it is.

django-channels databinding on model.save()

I have a channels app that is using databinding. When changes are made with django admin they are being pushed to the web as expected. I have loop set up on a socket connection to do some long polling on a gpio unit and update the db, these changes are not being pushed to the web. Channels documentation says:
Signals are used to power outbound binding, so if you change the values of a model outside of Django (or use the .update() method on a QuerySet), the signals are not triggered and the change will not be sent out. You can trigger changes yourself, but you’ll need to source the events from the right place for your system.
How do I go about triggering these changes, as it happens with admin?
Thanks and please let me know if this is to vague.
The relevant low-level code is in lines 121-187 of channels/binding/base.py (at least in version 1.1.6). That's where the signals are received and processed. It involves a few different things, such as keeping track of which groups to send the messages to. So it's a little involved, but you can probably tease out how to do it, looking at that code.
The steps involved are basically:
Find the right groups for the client
Format your message in the same way that the databinding code would (see this section of the docs)
Send the message to all the relevant groups you found in step 1.
Alternatively, you might consider using a REST API such that the socket code submits a POST to the API (which would create a database record via the ORM in the normal way) rather than directly creating database records. Your signals will happen automatically in that case. djangorestframework (server-side) and requests (client-side, if you're using python for the long-polling code) are your friends if you want to go that way, for sure. If you're using another language for the long-polling client, there are many equivalent packages for REST API client work.
Good luck!

Updating the state of data in the client in response to the work of other clients

So I have an application that could theoretically be used by multiple people at the same time, filling positions in a list of lists. The client app is in angular and is using a grid to display the lists of lists, the backend is in Django.
I'm having a hard time coming up with a way to signal client A, that client B did something. Multi-threading would let me do some long polling with locks and signals, but multi-processing makes this much more difficult.
How do I keep both (could be more than 2) of the clients up to date with the state/content of the list of lists as it is on the server?
Right now I'm restricting the number of users to 1, but this is not optimal.
You can make the clients pull changes from the server every X seconds, then the server can respond with changes list or with no change. This is not real realtime but if you make the pull delta low it will look like almost real time.
Alterantively, you can use websockets, but AFAIK, this is not supported by django, so you need to manage the websockets using tornado python or node.js or something like.
I would suggest using redis or other memory based datastore to communicate and propagate teh changes between the different servers.

How to make a superfast webserver for "check for updates"?

Which is the best approach for creating a fast response in case a client application asks webserver for "check for updates".
Skype for example takes about 1 second to answer. How to achieve the same?
I assume you are running one or more web servers and one or more back-end servers (with business logic).
One possible approach that I have seen: keep a change counter in webserver and when the back-end state changes, let the business logic notify all webservers with new change counter value.
Each web browser polls regularly the webserver for counter value and compares the value to the previous value. In case old_value != new_value, the web browser goes and asks the webserver for new content.
This allows the regular polling to be super-fast (1ms) and cheap. And only if something has really changed the browser will ask for more resource-expensive content generation.
The other option would be to use some asynchronous HTTP magic (cometd) but the approach outlined above is simpler, more understandable and easier to troubleshoot.
The simple approach is to just have a flat text or XML file on the server, containing the details of the most recent version. The client app fetches it via http GET, compares the version, and reacts accordingly. The http server is simply returning a small file, which is what http servers are designed to do. You should be able to handle hundreds of requests per second this way.
Use a large, distributed systems, depending on the number of your users. Put your web server(s) closer to clients, avoiding longer latencies. Use cluster and load balancing software to enhance performance. Use reverse proxies to cache data.
But is is really important that a "check for updates" is that fast? You can also check in a background thread. I would improve performance for other tasks.

Notifying web service consumer that some data has changed?

What would be a more standard way of notifying a web service consumer of a data change?
Having the consumer periodically calling the web service to pull change notification.
Consumer setting up a call back web service that can be invoked to forward notification about the change.
Other?
Both of these are options. There is also something called "comet" which is like setting up a stream between between the consumer and producer - messages can then be passed back and forth between the two. Wikipedia is probably the best place to start investigating to see if it will work for you project: http://en.wikipedia.org/wiki/Comet_(programming)
Depends on the scenario. If you're working in a closed environment with only a few consumers of your service, you could switch to a COMET style service which allows a callback from the service to the client. More about that here:
Wikipedia - COMET
From what I've read, that method doesn't scale well in larger environments so I'd be careful.
The more traditional method is your first option of polling the service for changes. As long as your service performs well and you have the appropriate hardware to serve up the requests, it's probably your best bet for a public facing web service.
In case you weren't aware of it, and in case it helps: WCF can work with a Duplex contract that in effect creates a callback service contract on the client. It's fairly transparent.