Listening to API changes in Flutter

3,950

Solution 1

What you need is not an API but a Webhook:

An API can be used from an app to communicate with myapi.com. Through that communication, the API can List, Create, Edit or Delete items. The API needs to be given instructions, though.

Webhooks, on the other hand, are automated calls from myapi.com to an app. Those calls are triggered when a specific event happens on myapi.com. For example, if a new user signs up on myapi.com, the automated call may be configured to ask the app to add a new item to a list.

Solution 2

is using sockets the only way to achieve this scenario ? or is it possible to do it in a normal http request ?

Sockets is only one of the ways to achieve your goal. It is possible to do it using a normal http request. Here, for example, the docs explain how to update data over the internet using HTTP.

From the flutter docs:

In addition to normal HTTP requests, you can connect to servers using WebSockets. WebSockets allow for two-way communication with a server without polling.

You'll find what you need under the networking section.

You should also take a look at the Stream and StreamBuilder classes.

Share:
3,950
Ahmed ibrahim
Author by

Ahmed ibrahim

Updated on December 22, 2022

Comments

  • Ahmed ibrahim
    Ahmed ibrahim over 1 year

    assume I have an API that gives a JSON response that return an id and a name.

    In a mobile application normally I would make an http GET response to get this data in a one time connection with the server and display the results in the app, however if the data changes over time and I want to keep listening to this data whenever it changes how is that possible ?

    I have read about sockets and seen the socket_io_client and socket_io packages, but I did not get my head around it yet, is using sockets the only way to achieve this scenario ? or is it possible to do it in a normal http request ?

    Thanks for your time