Skip to content

sanity-io/comlink

Repository files navigation

Comlink

CI

A monorepo containing packages for cross-origin communication between Window contexts, built on the postMessage API.

Overview

This repository provides TypeScript libraries for establishing and maintaining bidirectional communication between parent and child Window contexts (such as iframes and popups). It's designed to handle one-to-many connections with automatic connection management, heartbeat monitoring, and type-safe message passing.

Packages

The core library for one-to-many cross-origin communication between Window contexts.

Key Features:

  • πŸ”„ Automatic connection establishment and recovery
  • πŸ’“ Heartbeat monitoring for connection health
  • 🎯 Type-safe message passing with TypeScript
  • πŸ“‘ Request/response pattern support
  • πŸ”Œ One-to-many channel management
  • πŸ›‘οΈ Origin validation for security

Installation:

npm install @sanity/comlink

Quick Example:

// Parent window
import {createController} from '@sanity/comlink'

const controller = createController({targetOrigin: 'https://child-origin.com'})
const channel = controller.createChannel({name: 'parent', connectTo: 'child'})

channel.post('greeting', {message: 'Hello!'})

// Child window
import {createNode} from '@sanity/comlink'

const node = createNode({name: 'child', connectTo: 'parent'})
node.on('greeting', (data) => console.log(data.message))

πŸ“– Full Documentation

⚠️ Internal Package - This package provides specialized message types and compatibility utilities for communication between Sanity's presentation tool and visual editing applications.

This package is used internally by:

  • sanity/presentation
  • @sanity/visual-editing
  • @sanity/core-loader
  • next-sanity/live
  • @sanity/svelte-loader
  • @sanity/react-loader

πŸ“– Documentation

Development

This is a monorepo managed with pnpm workspaces and Turborepo.

Prerequisites

  • Node.js >= 18 (Node.js 22 required for playground app)
  • pnpm 10.18.2

Getting Started

# Install pnpm if you don't have it
npm install -g [email protected]

# Install dependencies
pnpm install

# Build all packages
pnpm build

# Run tests
pnpm test

# Run linter
pnpm lint

# Format code
pnpm format

# Type check
pnpm type-check

Development Workflow

# Watch mode for development (also runs the playground app)
pnpm dev

The playground app is also deployed to Vercel and available at https://comlink-playground.sanity.dev/

Project Structure

.
β”œβ”€β”€ apps/
β”‚   └── playground/          # Example application for testing
β”œβ”€β”€ packages/
β”‚   β”œβ”€β”€ comlink/            # Core communication library
β”‚   └── presentation-comlink/ # Sanity-specific utilities
└── .changeset/             # Changeset configuration for releases

Publishing

This repository uses Changesets for version management and publishing.

To release a new version:

  1. Create a changeset describing your changes:
    pnpm changeset add
  2. Open a PR with your changes and the changeset
  3. Once merged, a "Version Packages" PR will be automatically created
  4. Merge the "Version Packages" PR to publish the new version

Architecture

Controllers & Channels

Controllers manage one or more Channels that communicate with Nodes in child windows. Each Channel can connect to multiple Nodes across different Window contexts.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     Parent Window             β”‚
β”‚                               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                 β”‚
β”‚  β”‚Controllerβ”‚                 β”‚
β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜                 β”‚
β”‚       β”‚                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚ Channel 1 β”‚  β”‚Channel 2 β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚             β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”
    β”‚ iframe  β”‚   β”‚ popup  β”‚
    β”‚  Node   β”‚   β”‚  Node  β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Connection States

Connections progress through the following states:

  • idle β†’ handshaking β†’ connected β†’ disconnected

Contributing

Contributions are welcome! Please read the code and follow existing patterns when contributing.

Code Quality

  • All code must pass TypeScript type checking
  • Follow existing code style (enforced by Prettier)
  • Ensure linter passes (oxlint with type-aware checks)
  • Add tests for new functionality

License

MIT License Β© 2016-2025 Sanity.io

See LICENSE for more details.

Resources

Related Projects

About

Robust http-like coms over iframes and windows using postMessage

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages