npm package: @rqsts/react-realtime
Description: A saga enabling realtime updates for @rqsts/react-app
Latest version: 1.0.20

# @rqsts/realtime

A custom saga enabling realtime update inside [@rqsts/react-app](https://github.com/marmelab/@rqsts/react-app/).

## Installation

Install with:

```sh
npm install --save @rqsts/realtime
```

or

```sh
yarn add @rqsts/realtime
```

## Usage

Define an `observeRequest` function which will be called by the realtime saga whenever a `CRUD_GET_LIST` or `CRUD_GET_ONE` fetch
is triggered by @rqsts/react-app ([documentation](https://marmelab.com/@rqsts/react-app/DataProviders.html) about those).

This function will be called with the following parameters:

- `fetchType`: either `CRUD_GET_LIST` or `CRUD_GET_ONE`
- `resource`: the resource's name
- `params`: the fetch parameters
  - for `CRUD_GET_LIST`: `{ pagination: { page: {int} , perPage: {int} }, sort: { field: {string}, order: {string} }, filter: {Object} }`
  - for `CRUD_GET_ONE`: `{ id: {mixed} }`

This function must return an object with a `subscribe` method which will be called with an `observer`. If it returns `null`, the query won't be updated automatically. This allows you to decide which query should be updated in real time.

The `observer` have the following methods:

- `next(data)`: Call this method each time new data is received so that the @rqsts/react-app views are updated.
- `complete()`: Call this method to indicates this subscription won't receive any new data.
- `error(error)`: Call this method when an error occurs.

The `subscribe` method must return a `subscription` object. The `subscription` object must have an `unsubscribe` method which will be called by the realtime saga when the query will not need to be observed anymore. This will happen each time the current route change and will give you the opportunity to clean up related sockets, apollo observable queries, etc. When called and after you cleaned up whatever needed cleaning, you **must** call the `observer.complete` method so that the realtime saga is notified about it.

Here is a very naive example using an interval to fetch data every 5 seconds:

```js
// In createRealtimeSaga.js
import realtimeSaga from @rqsts-react-realtime;

const observeRequest = dataProvider => (type, resource, params) => {
    // Filtering so that only posts are updated in real time
    if (resource !== 'posts') return;

    // Use your apollo client methods here or sockets or whatever else including the following very naive polling mechanism
    return {
        subscribe(observer) {
            const intervalId = setInterval(() => {
                dataProvider(type, resource, params)
                    .then(results => observer.next(results)) // New data received, notify the observer
                    .catch(error => observer.error(error)); // Ouch, an error occured, notify the observer
            }, 5000);

            const subscription = {
                unsubscribe() {
                    // Clean up after ourselves
                    clearInterval(intervalId);
                    // Notify the saga that we cleaned up everything
                    observer.complete();
                }
            };

            return subscription;
        },
    };
};

export default dataProvider => realtimeSaga(observeRequest(dataProvider));
```

This saga factory can be used like this:

```jsx
import React from "react";
import { Admin, Resource } from "@rqsts/react-app";

import createRealtimeSaga from "./createRealtimeSaga";
import dataProvider from "./dataProvider";
import posts from "./posts";

const realTimeSaga = createRealtimeSaga(dataProvider);

render(
    <Admin
        dataProvider={dataProvider}
        customSagas={[realTimeSaga]}
    >
        <Resource name="posts" {...posts} />
    </Admin>,
    document.getElementById("root")
);
```