Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP feat(experimental): MultiShapeStream and TransactionalMultiShapeStream APIs #2356

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

samwillis
Copy link
Contributor

@samwillis samwillis commented Feb 19, 2025

WIP - Opening for visibility + discussion.

This adds two new APIs to the @electric-sql/experimental package:

MultiShapeStream

A multi-shape stream is a stream that can subscribe to multiple shapes. It ensures that all shapes will receive at least an up-to-date message from Electric within the checkForUpdatesAfter interval.

The emitted messages have an additional shape prop that is the name of the shape provided in the shapes object when constructing the MultiShapeStream.

const multiShapeStream = new MultiShapeStream({
  shapes: {
    shape1: {
      url: 'http://localhost:3000/v1/shape1',
    },
    shape2: {
      url: 'http://localhost:3000/v1/shape2',
    },
  },
})

multiShapeStream.subscribe((msgs) => {
  console.log(msgs)
})

// or with ShapeStream instances
const multiShapeStream = new MultiShapeStream({
  shapes: {
    shape1: new ShapeStream({ url: 'http://localhost:3000/v1/shape1' }),
    shape2: new ShapeStream({ url: 'http://localhost:3000/v1/shape2' }),
  },
})

TransactionalMultiShapeStream

A transactional multi-shape stream is a multi-shape stream that emits the messages in transactional batches.
It uses the lsn metadata to infer transaction boundaries, and the op_position metadata to sort the messages within a transaction.

Note that you will only receive change massages on the main subscription (in transactional batches), and not any control massages.

const transactionalMultiShapeStream = new TransactionalMultiShapeStream({
  shapes: {
    shape1: {
      url: 'http://localhost:3000/v1/shape1',
    },
    shape2: {
      url: 'http://localhost:3000/v1/shape2',
    },
  },
})

transactionalMultiShapeStream.subscribe((msgs) => {
  console.log(msgs)
})

// or with ShapeStream instances
const transactionalMultiShapeStream = new TransactionalMultiShapeStream({
  shapes: {
    shape1: new ShapeStream({ url: 'http://localhost:3000/v1/shape1' }),
    shape2: new ShapeStream({ url: 'http://localhost:3000/v1/shape2' }),
  },
})

To to:

  • Explore adding additional shapes after the initial multi-shape stream is created.

@samwillis samwillis force-pushed the samwillis/multi-shape-stream branch 2 times, most recently from ed28547 to 9cd5cc1 Compare February 19, 2025 11:14
@samwillis samwillis force-pushed the samwillis/multi-shape-stream branch from 9cd5cc1 to b5e3303 Compare February 19, 2025 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant