This is a Proof of Concept (POC) demonstrating the implementation of Change Streams in Meteor, based on Pull Request #13787.
This project demonstrates the usage of Meteor's new streaming APIs:
Meteor.publish.stream- Publishing change streams on the serverMeteor.subscribeStream- Subscribing to streams on the client
⚠️ API Note: ThesubscribeStreamAPI will be transformed tosubscribe.streamin the next commits.
This Developer Experience (DX) was inspired by the excellent Jam pub/sub package, which already implements similar patterns for change streams.
In the server/main.js file, we use Meteor.publish.stream to create a publication that monitors changes in the collection:
Meteor.publish.stream('streamLinks', function (sub) {
const changeStream = LinksCollection.watch([
{ $match: { operationType: 'insert' } }
]);
changeStream.on('change', (change) => {
sub.stream(change);
});
sub.onStop(() => changeStream.close());
});Features:
- Uses MongoDB Change Streams natively
- Allows specific filters (in the example, only
insertoperations) - Automatically manages connection lifecycle
- Calls
sub.stream(data)to send data to clients
In the imports/ui/Info.jsx file, we use Meteor.subscribeStream to receive real-time events:
useEffect(() => {
// Subscribe to the change stream
const sub = Meteor.subscribeStream('streamLinks');
sub.onData((data) => {
setStreamEvents(evts => [data, ...evts]);
});
return () => sub.stop();
}, []);Features:
- Simple and reactive API
onDatacallback to process received events- Automatic cleanup with
sub.stop() - Perfect integration with React hooks
├── client/ # Meteor client
├── server/ # Meteor server
├── imports/
│ ├── api/
│ │ └── links.js # Collection and methods
│ └── ui/
│ ├── App.jsx # Main component
│ ├── Hello.jsx # Button to insert data
│ └── Info.jsx # Change events display
├── mongo-replica/ # MongoDB replica set data
└── package.json
- Meteor.js
- MongoDB (configured as replica set for change streams)
-
Start the MongoDB replica set:
./start-meteor-mongo-replica.sh
-
Run the Meteor application:
meteor run
-
Test the functionality:
- Click the "Click Me" button to insert new links
- Watch the change events appear in real-time in the "Change Stream Events" section
-
To stop MongoDB:
./stop-meteor-mongo-replica.sh
Change Streams are ideal for:
- Real-time notifications - Notify users about relevant changes
- Audit and logging - Track all operations on specific collections
- Synchronization - Keep external systems synchronized
- Analytics - Process data events in real-time
- Cache invalidation - Invalidate caches when data changes