I’ve always been curious, it’s tough to find a good ELI5 for pub/sub.
I understand as it pertains to Meteor is that DDP works with a Websocket to Track changes and push them over to client and vice-versa.
But internally, how does the client know to get a change from server? Does the client constantly poll? Or is it more that the server just pushes to the client frequently?
In it’s most basic form, the meteor server (all of them) watch the database, using the exact same mechanism as a secondary database member in the replicaset, by watching a collection on the database which is called the oplog (operations log). Using this mechanism each meteor server observes all changes against all documents in all collections on the database.
When a client connects to a meteor server and subscribes to data, the server keeps track of which documents (by means of the selector) and which fields on those documents (by means of the fields projection) that client cares about.
When the server receives an “oplog notification” it looks at which connected clients (if any) care about that field of that document (if an update) or just care about the document at all (In the case of insert/remove). It will then, generally, repoll the database and send a minimal diff of the changes (e.g., the new values of the changed fields) to the client. It does this via something called a mergebox. Which tracks the values a client currently has per document, which allows denouncing updates that the client doesn’t need - this piece causes a lot of confusion as it works at the top level, not recursively through fields.
Nowadays, most projects use redis oplog - which works in the same way, except that the servers register interest against specific documents (in the case of id based selectors) or the entire collection (in the case of arbitrary selectors) or based on specified channels (for enhanced performance at the cost of more configuration). Then the meteor servers tell redis about the update in parallel to telling mongo.