A simple way to have realtime using Websocket with cozy-stack.
npm install --save cozy-realtime
or
yarn add cozy-realtime
This method allow you to subscribe to realtime for all documents of a provided doctype. Here are the parameters:
config
: a config object with the following keys :domain
: the instance domain (ex:cozy.works
,cozy.tools:8080
), must be provided ifurl
is not settoken
: the cozy token (ex: the globalcozy.client._token.token
)url
: the cozy url (ex: https://recette.cozy.works), must be provided ifdomain
is not setsecure
: a boolean indicating if a secure protocol must be used (defaulttrue
). Should not be provided alongurl
.
doctype
: the doctype to subscribe (ex:io.cozy.accounts
)parse
: a custom function to be use as parser for your resulting documents (default:doc => doc
)
Here is an example:
import realtime from 'cozy-realtime'
const subscription = await realtime.subscribeAll(cozy.client, 'io.mocks.mydocs')
// your code when a new document is created
subscription.onCreate(doc => doSomethingOnCreate(doc))
// your code when a document is updated
subscription.onUpdate(doc => doSomethingOnUpdate(doc))
// your code when a document is deleted
subscription.onDelete(doc => doSomethingOnDelete(doc))
// Unsubscribe from realtime
subscription.unsubscribe()
This method is exactly the same working as the previous subscribeAll()
function but to listen only one document. Here are the parameters:
config
: a config object with the following keys :domain
: the instance domain (ex:cozy.works
,cozy.tools:8080
), must be provided ifurl
is not settoken
: the cozy token (ex: the globalcozy.client._token.token
)url
: the cozy url (ex: https://recette.cozy.works)secure
: a boolean indicating if a secure protocol must be used (defaulttrue
). Should not be provided alongurl
.
doctype
: the doctype to subscribe (ex:io.cozy.accounts
)doc
: the document to listen, it must be at least a JS object with the_id
attribute of the wanted document (only the_id
will be checked to know if this is the wanted document or not).parse
: a custom function to be use as parser for your resulting documents (default:doc => doc
)
Here is an example:
import realtime from 'cozy-realtime'
const subscription = await realtime.subscribe(cozy.client, 'io.mocks.mydocs', myDoc)
// your code when your document is updated
subscription.onUpdate(doc => doSomethingOnUpdate(doc))
// your code when your document is deleted
subscription.onDelete(doc => doSomethingOnDelete(doc))
// Unsubscribe from realtime
subscription.unsubscribe()
[Cozy] is a platform that brings all your web services in the same private space. With it, your webapps and your devices can share data easily, providing you with a new experience. You can install Cozy on your own hardware where no one's tracking you.
You can reach the Cozy Community by:
- Chatting with us on IRC [#cozycloud on Freenode][freenode]
- Posting on our [Forum][forum]
- Posting issues on the [Github repos][github]
- Say Hi! on [Twitter][twitter]
The maintainers for Cozy Realtime are Greg and kosssi !
cozy-realtime
is distributed under the MIT license.