Skip to content

Commit

Permalink
feat: Rewrite of realtime
Browse files Browse the repository at this point in the history
BREAKING CHANGES: There should be no breaking changes, but as it is a full rewrite, let's act as if it does.

Among the changes :

- Realtime now correctly reconnect if the computer goes to sleep, whatever the time gone offline.

- We do not try on attempt every 200ms 60 times anymore. There is an exponential backoff mechanism. The reconnection may take up to 5 minutes (worst case scenario).

- It now allows to specify an id when subscribing to the CREATED event. This was needed for cozy-notes.

- It allows to subscribe to all events for a doctype (and optionally an id). The socket with the stack was subscribing to all events anyways.

- The code is longer but should be way simplier. I deleted the extra layer with  Socket.js which was mostly boilerplate and additional complexity for asynchroneous events.

- The subscriptions are now handled by a specific class instead of doing shameful magic with the internal of MicroEE.

- The retry logic has an external class, easying to tweak the values in the future.

- There are Minilog logs on level info or debug that can be activated when needed (same inside the main test file, because sometimes it may help). Only warnings are activated by default.

- Deprecation of CozyRealtime.send() (which was not sending something on the websocket, despites its name) in favor of CozyRealtime.sendNotification()
  • Loading branch information
edas committed Feb 24, 2020
1 parent 212f2d7 commit 1022af7
Show file tree
Hide file tree
Showing 15 changed files with 2,392 additions and 832 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ exports[`TriggerManager when given an oauth konnector should redirect to OAuthFo
flow={
ConnectionFlow {
"client": CozyClient {
"_events": Object {
"login": Array [
[Function],
],
"logout": Array [
[Function],
],
"tokenRefreshed": Array [
[Function],
],
},
"appMetadata": Object {},
"chain": CozyLink {
"request": [Function],
Expand Down Expand Up @@ -136,19 +125,7 @@ exports[`TriggerManager when given an oauth konnector should redirect to OAuthFo
"handleTwoFA": [Function],
"launch": [Function],
"realtime": CozyRealtime {
"_beforeUnload": [Function],
"_cozyClient": CozyClient {
"_events": Object {
"login": Array [
[Function],
],
"logout": Array [
[Function],
],
"tokenRefreshed": Array [
[Function],
],
},
"client": CozyClient {
"appMetadata": Object {},
"chain": CozyLink {
"request": [Function],
Expand Down Expand Up @@ -261,35 +238,36 @@ exports[`TriggerManager when given an oauth konnector should redirect to OAuthFo
"uri": "",
},
},
"_events": Object {},
"_receiveError": [Function],
"_receiveMessage": [Function],
"_resetSocket": [Function],
"_resubscribe": [Function],
"_retryDelay": 1000,
"_retryLimit": 60,
"_socket": Socket {
"_doctype": "io.cozy.websocket",
"onClientLogin": [Function],
"onClientLogout": [Function],
"onClientTokenRefreshed": [Function],
"onDeviceReady": [Function],
"onOffline": [Function],
"onOnline": [Function],
"onResume": [Function],
"onWebSocketClose": [Function],
"onWebSocketError": [Function],
"onWebSocketMessage": [Function],
"onWebSocketOpen": [Function],
"retryManager": RetryManager {
"_events": Object {
"close": Array [
[Function],
],
"error": Array [
[Function],
],
"message": Array [
[Function],
],
},
"_getToken": [Function],
"_getUrl": [Function],
"_token": null,
"_url": null,
"_webSocket": null,
"removeAllListeners": [Function],
"baseWaitAfterFirstFailure": 128,
"maxWaitBetweenRetries": 300000,
"onFailure": [Function],
"onSuccess": [Function],
"onSuccessAcknowledged": [Function],
"raiseErrorAfterAttempts": 8,
"retries": 0,
"timeBeforeSuccessful": 1200,
"wait": 0,
},
"subscriptions": SubscriptionList {
"subscriptions": Array [],
},
"_updateAuthentication": [Function],
"unsubscribeAll": [Function],
},
"sendTwoFACode": [Function],
"state": Object {
Expand Down
4 changes: 4 additions & 0 deletions packages/cozy-realtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ A simple way to have realtime using Websocket with [cozy-stack](https://github.c
or
`yarn add cozy-realtime`

### Cordova

In order to listen to online and offline events, you should install the [Network Information](https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-network-information/) plugin.

## Example

```js
Expand Down
1 change: 1 addition & 0 deletions packages/cozy-realtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
]
},
"dependencies": {
"cozy-device-helper": "^1.9.0",
"minilog": "https://github.com/cozy/minilog.git#master"
}
}

0 comments on commit 1022af7

Please sign in to comment.