Skip to content

8. Live updates ​

The order list of step 7 is a live list. useMongocampLive loads the orders and keeps them current: an order placed in another tab, by another client or by server code appears without a reload.

vue
// the orders of the user (owner), with the customer of each order (expand), updated when they change
const orders = useMongocampCollection('orders')
const live = useMongocampLive('orders', { expand: ['customerId'] })
live.documentsthe orders, typed like a find with the same options
live.statusconnecting, live, polling, error, stopped
live.errorthe error of the last load or event
live.start(), live.stop(), live.reload()control it yourself (immediate: false to start later)

The server sends the changes as Server-Sent Events, scoped like every read: a user gets events only for orders they can read.

  • patch (default) applies each change to the list. A new order on a full page (rowsPerPage) loads the page again instead.
  • refetch (default with sort or page) loads the page again after changes, at most every 200 ms.

Without a replica set there are no change streams. The server answers 501, and the list loads every few seconds instead (pollInterval). Your code does not change. For a local replica set:

bash
docker run -d -p 27017:27017 mongo:7 --replSet rs0
docker exec -it <container> mongosh --eval "rs.initiate()"

Two limits to know:

  • A change of a referenced document sends no event of the order. Rename a customer, and the live order list shows the old name until that order changes or the page reloads.
  • Outside a component (in a store) call live.stop() yourself. In a component it stops on unmount.

Released under the MIT License.