Pub/Sub (Publish-Subscribe)
A messaging pattern where publishers send messages to topics, and subscribers receive messages from topics they're interested in, enabling decoupled, asynchronous communication.
Infrastructure
Pub/Sub is a messaging pattern enabling asynchronous communication between components through topics, allowing publishers and subscribers to operate independently.
Architecture
Publishers Topic/Broker Subscribers
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Service A│───────────►│ Topic: │──────────►│ Service B│
└──────────┘ │ events │ └──────────┘
┌──────────┐ │ │ ┌──────────┐
│ Service C│───────────►│ │──────────►│ Service D│
└──────────┘ └──────────┘ └──────────┘
Example
// Publisher
await pubsub.publish('entity-created', {
entityId: 'person_123',
type: 'Person',
properties: {name: 'Jane'}
});
// Subscriber
pubsub.subscribe('entity-created', async (message) => {
console.log('New entity:', message.entityId);
// Update search index, send notification, etc.
});
TrustGraph Events
// TrustGraph uses Apache Pulsar for pub/sub
await trustgraph.events.subscribe('graph.node.created', async (event) => {
// React to new nodes
console.log('Node created:', event.nodeId);
});
await trustgraph.events.subscribe('graph.relationship.created', async (event) => {
// React to new relationships
console.log('Relationship created:', event.relationshipType);
});
Benefits
- Decoupling: Publishers don't know subscribers
- Scalability: Add subscribers without changing publishers
- Asynchronous: Non-blocking communication
- Fan-out: One message, many subscribers
See Also
Examples
- •Apache Pulsar for TrustGraph event streaming
- •Publishing 'entity_created' events when new graph nodes are added