.put()
The.put() method writes data to the graph. It handles conflict resolution using GUN’s CRDT algorithm and automatically syncs changes to connected peers.
Signature
Parameters
The data to write. Can be:
- Object: Creates or updates a node with properties
- Primitive: Stores a scalar value (string, number, boolean, null)
- Function: Deferred write - function receives callback to pass data
- Link: Another GUN reference (creates a relationship)
.set() for collections.Called after the write is acknowledged. Receives an acknowledgment object.Signature:
function(ack)ack.err- Error object if write failedack.ok- Success confirmationack['@']- Acknowledgment ID
Advanced options for write behavior
Return Value
Returns the same GUN chain reference for method chaining.Examples
Write Object Data
Update Individual Properties
Write with Callback
Deferred Write with Function
Delete Properties
Create Relationships
Implementation Details
From the source code (put.js:4-78):.put() method:
- Validates the data structure
- Walks the graph to find nested objects
- Assigns state timestamps for conflict resolution
- Handles circular references
- Emits updates to connected peers
- “Stuns” reads during writes to prevent race conditions
Conflict Resolution
GUN uses state-based CRDTs for automatic conflict resolution:Valid Data Types
From valid.js, GUN supports:null(for deletes)stringnumber(except ±Infinity)boolean- Objects (nested graphs)
- Soul references:
{ '#': 'node-id' }
Invalid Data
Notes
- Writes are asynchronous and eventually consistent
- Data is automatically synchronized to all connected peers
- Partial updates merge with existing data (they don’t replace it)
- Circular references are automatically detected and handled
- State timestamps are automatically generated using Gun.state()