Replicated entities

This page documents how to implement Cloudstate Replicated Entities in Javascript. For information on what Cloudstate Replicated Entities are, please read how to choose a state model and the general Replicated Entity information first.

A Replicated Entity can be created with the following code.

const crdt = require("cloudstate").crdt;

const entity = new crdt.Crdt(

Init default value of Replicated Entity

We can init the state like the following code

entity.defaultValue = () => new crdt.ORSet();

Handling commands

By default, the name of the command that the method handles will be the name of the method with the first letter capitalized. So, a method called getFriends will handle a gRPC service call command named GetFriends.

The command handler also can take the gRPC service call input type as a parameter to receive the command message. This is optional, sometimes it’s not needed.

The return type of the command handler must be the output type for the gRPC service call, this will be sent as the reply.

The following shows the implementation of the command handlers. In the following example, the command handlers add and remove update the state in Replicated Entity:

function add(friend, ctx) {
  return {};

function remove(friend, ctx) {
  return {};

function getFriends(user, ctx) {
  console.log("getFriends for user: "+ user, {
    friends: Array.from(ctx.state)
  return {
    friends: Array.from(ctx.state)

entity.commandHandlers = {
  Add: add,
  Remove: remove,
  GetFriends: getFriends

Streamed command handlers

Replicated Entities support streaming over gRPC. Streamed commands can be used to receive and publish updates to the state. The detail is in CrdtCommandContextnew tab.

We provide a full chat app example including frontend and backend. Please refer to the full example in js-chat samplenew tab

Referral Docs