Subscriptions/real-time API support
Problem
Unfortunately at the current point of time Prisma 2 doesn't yet support subscriptions (real-time API features). Prisma 1 followed a different architecture which provided support for subscriptions. We might be planning to bring back real-time/subscriptions functionality in the future based on a standalone "events engine" (which will use CDC under the hood). This issue tracks the progress for this feature. Please feel free to share your specific requirements and wishes for this feature. ๐
Unverified for your environment
Select your OS to check compatibility.
2 Fixes
Implement Event Engine for Real-Time Subscriptions in Prisma
Prisma 2 currently lacks built-in support for real-time subscriptions due to its architecture, which differs from Prisma 1. The absence of an events engine means that changes in the database cannot be pushed to clients in real-time, limiting the application's interactivity and responsiveness.
Awaiting Verification
Be the first to verify this fix
- 1
Design the Events Engine Architecture
Define the architecture for the standalone events engine that will handle change data capture (CDC) and manage real-time subscriptions. This should include the components responsible for listening to database changes and broadcasting updates to clients.
javascriptconst { EventEmitter } = require('events'); class EventsEngine extends EventEmitter { constructor() { super(); // Initialize CDC listener } listenToChanges() { // Logic to listen to database changes } broadcastUpdate(data) { this.emit('update', data); } } - 2
Integrate CDC with PostgreSQL
Utilize PostgreSQL's logical replication or triggers to capture changes in the database. This will allow the events engine to receive updates whenever data is modified, ensuring that clients can receive real-time notifications.
sqlCREATE TRIGGER notify_changes AFTER INSERT OR UPDATE OR DELETE ON your_table_name FOR EACH ROW EXECUTE FUNCTION notify_event(); - 3
Implement WebSocket Server for Client Communication
Set up a WebSocket server that will allow clients to subscribe to specific events. When the events engine broadcasts an update, the WebSocket server will push this update to all connected clients.
javascriptconst WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', function connection(ws) { ws.on('message', function incoming(message) { // Handle client subscription }); }); - 4
Create Client-Side Subscription Logic
Develop the client-side logic to connect to the WebSocket server and handle incoming updates. This will allow the application to react to changes in real-time, providing a seamless user experience.
javascriptconst socket = new WebSocket('ws://localhost:8080'); socket.onmessage = function(event) { const data = JSON.parse(event.data); // Update UI with new data }; - 5
Test and Validate the Real-Time Functionality
Conduct thorough testing to ensure that the events engine correctly captures changes and that the WebSocket server reliably pushes updates to clients. Validate that all scenarios (insert, update, delete) are handled appropriately.
javascript// Test cases to validate real-time updates expect(data).toEqual(expectedData);
Validation
Confirm the fix by testing the application to ensure that real-time updates are received by clients when changes occur in the database. Monitor the WebSocket connections and verify that all expected events are emitted and handled correctly.
Sign in to verify this fix
1 low-confidence fix
Implement Event-Driven Architecture for Real-Time Subscriptions
Prisma 2 does not currently support real-time subscriptions due to its architectural design, which differs from Prisma 1. The absence of an events engine limits the ability to push updates to clients in real-time, as there is no mechanism to listen for changes in the database.
Awaiting Verification
Be the first to verify this fix
- 1
Design Event-Driven Architecture
Outline the architecture for an event-driven system that can handle real-time subscriptions. This should include defining the event sources, event types, and how they will be processed.
N/AN/A - 2
Integrate Change Data Capture (CDC)
Implement a Change Data Capture mechanism that listens to database changes. This can be achieved using tools like Debezium or custom triggers in PostgreSQL to capture changes and publish events to a message broker.
sqlCREATE TRIGGER my_trigger AFTER INSERT OR UPDATE OR DELETE ON my_table FOR EACH ROW EXECUTE FUNCTION notify_event(); - 3
Set Up a Message Broker
Choose and set up a message broker (e.g., Kafka, RabbitMQ) to handle the events generated by the CDC mechanism. This broker will facilitate communication between the database and the clients.
N/AN/A - 4
Develop Subscription API Endpoints
Create API endpoints that allow clients to subscribe to specific events. This could be implemented using WebSockets or Server-Sent Events (SSE) to push updates to clients in real-time.
javascriptconst WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', function connection(ws) { // Logic to send events to the client }); - 5
Test Real-Time Functionality
Conduct thorough testing to ensure that the real-time subscription functionality works as intended. This includes testing the event flow from the database to the client and ensuring that updates are received in real-time.
N/AN/A
Validation
Confirm that clients receive real-time updates when changes occur in the database. This can be tested by making changes to the database and observing if the subscribed clients receive the corresponding events.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep