# Data collaboration

## Function introduction

In some business scenarios, data changes are only perceived by refreshing the page, but in some business scenarios, there are data changes that can be coordinated in real time.

Block provides data collaboration capabilities. It should be noted that the collaboration capabilities are optional, and developers should choose access according to their demands. For example, in cloud documents, after a user operates a block, other users can also see that the block has changed.

We provide two types of data collaboration capabilities:

1. BlockEntity data collaboration. That is, with the same blockID, different users receive the same push content.
2. User dimension data collaboration. That is, with the same blockID, some users can receive the push, and the push content received by different users can be different.

## BlockEntity data collaboration

BlockEntity data collaboration is mainly aimed at pushing non-personalized data, that is, online blocks with the same blockID will receive data pushes with the same content. For example, in the voting block, when the voting content changes, all online devices using the blockID will receive the same voting result information.

### Business Process

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/429329d7b4de7a5a82e2d5954e8cac0e_S2ynU3V7bZ.png?lazyload=true&width=1640&height=649)

The developer server needs to map the relationship between the business id and blockID, because the change push is based on the blockID as the primary key.
The specific binding method is: in the [server creation process](https://open.feishu.cn/document/uAjLw4CM/uYjL24iN/block/service/create), the developer server sends the business data to the open platform, and the open platform returns the blockID, so as to develop The client server can bind the business id to the blockID.

### Access collaboration

#### Client listens for changes

Developers need to monitor the push of data in Block. When the developer pushes the updated data, the Block framework will receive the data push through the open platform, and provide the data to the developer Block through the `onResourceChange` life cycle.
Sending an event on the server will determine the version, so the data received by `onResourceChange` must be in order.

The sample code is as follows. For detailed code, please refer to [Block](https://open.feishu.cn/document/uAjLw4CM/uYjL24iN/block/block-frame/framework-interface/block).

```js
Block({
  // Here sourceData can get the pushed data
  onResourceChange(sourceData) {
    console.log(sourceData);
  },
});
```

#### Server push updates

For the specific calling api, please refer to [Update BlockEntity](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/block-v2/entity/update).

## User dimension data collaboration

BlockEntity data collaboration cannot solve the demands for data push in some scenarios, so it also provides user-dimensional data collaboration. Including but not limited to the following scenarios:
- User-oriented change push, such as message badge, a small number of users can receive the push at the same time, and the data is different.
- Personalized data of thousands of people, such as subscription news, users can receive push notifications with different content at the same time.

### Business Process

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/3cb6c4de3358fa3104e5e7814ab85455_t2XqZgiIQs.png?lazyload=true&width=1640&height=576)

Only if it is in the specified user list, the device with the specified blockID online can receive the message push, that is, the specified user with the specified blockID offline will not receive the message.

### Service Access
#### Client listens for changes

Developers need to monitor the push of data in Block. When the developer pushes the updated data, the Block framework will receive the data push through the open platform, and provide the data to the developer Block through the onTypedMessage life cycle.
- Sending an event on the server will determine the version, and when the version received by the client is smaller than the local version, the change will be discarded.
- blockit-sdk version requirements: 1.5.0 and above.
The sample code is as follows:
```JavaScript
Block({
  onTypedMessage(message) {
    console.log(message.type, message.data);
    // business logic, such as updating the view
  }
});
```

#### Server push message

The specific calling api [Block message push](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/block-v2/message/create).
Regarding the trigger timing, two conditions need to be met at the same time:
- External message push
- After the app is launched

In order to ensure that messages can be pushed to the Block service normally, try to choose to push messages after the app is started or pull messages directly after the app is started.