Skip to content

Latest commit

 

History

History
 
 

cozy-realtime

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Cozy Realtime


What's cozy-realtime?

A simple way to have realtime using Websocket with cozy-stack.

Setup

Install

npm install --save cozy-realtime or yarn add cozy-realtime

API

subscribeAll(config, doctype, parse)

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 if url is not set
    • token: the cozy token (ex: the global cozy.client._token.token)
    • url: the cozy url (ex: https://recette.cozy.works), must be provided if domain is not set
    • secure: a boolean indicating if a secure protocol must be used (default true). Should not be provided along url.
  • 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()

subscribe(config, doctype, doc, parse)

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 if url is not set
    • token: the cozy token (ex: the global cozy.client._token.token)
    • url: the cozy url (ex: https://recette.cozy.works)
    • secure: a boolean indicating if a secure protocol must be used (default true). Should not be provided along url.
  • 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()

Community

What's Cozy?


[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.

Get in touch

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]

Maintainers

The maintainers for Cozy Realtime are Greg and kosssi !

License

cozy-realtime is distributed under the MIT license.