# ws.state.update

Shallow-merges the object --value into the object at --key, creating it if absent

# ws.state.update

```
ws.state.update --key TEXT --value JSON [--room TEXT] [--batch] [--visible-to TEXT…] → state
```

Shallow-merges the object `--value` into the object at `--key`, creating it if absent. The room's shared state is what every client of the room mirrors. `--key` is a JSON pointer; a part from the payload is written with {{ }}, and a segment that resolves empty is refused. `--room` is an exact room (a room that is not open is `FW_NODE_WS_ROOM_NOT_OPEN`); without it the room is the one of the [`trigger.room`](/nodes/trigger/trigger.room.md) that started the run. A change past the room's `--max-state` is refused. Clients get a `patch` of only what changed at once, or with `--batch` on the room's 33 ms tick (in a tick room, after the tick). `--visible-to <tag>` (repeatable; put and update) limits the path and all under it to the sessions holding one of the tags ([`ws.session.tag`](/nodes/ws/ws.session.tag.md)) — fog of war, a private hand: the others are never sent it. A put without it makes the path visible to all; an update without it keeps its limit. Adds `state: { key, room }` and keeps the rest of the payload. Sending an event without changing state is [`ws.message.send`](/nodes/ws/ws.message.send.md).

## Flags

- `--key` (required): A JSON pointer into the room's state: /players/{{ input.room.session_id }}. A segment that resolves empty is refused.
- `--value` (required): An object to merge — {{ expr }}.
- `--room`: The exact room; default: the room of the trigger.room that started the run.
- `--batch`: Let the room's 33 ms tick send the change, at most once a tick — for 10+ changes a second.
- `--visible-to`: Only sessions holding one of these tags see this path (repeat for several; none: as limited before). Tags come from ws.session.tag.

## Examples

### Move a player

```
| ws.state.update --key "/players/{{ input.room.session_id }}" --value "{{ { x: input.room.payload.x, y: input.room.payload.y } }}" --batch
```

Every client in the room sees the same `state.players`; `--batch` sends it at most 30 times a second.

It answers:

```json
{
  "state": {
    "key": "/players/s_8f2",
    "room": "lobby"
  }
}
```

## Used in

- [Real-Time Game (WebSocket State Sync)](/examples/realtime-game.md)
