Distributed Object Protocol is a thin layer on top of your data network that helps you communicate server and clients (nodes) using RPCs. It is also a pattern that makes easy update, mutate or even sync the state of your App using Patches.
// Serverconst{ createNode }=require('dop')constWebSocket=require('ws')constwss=newWebSocket.Server({port: 8080})constsum=(a,b)=>a+bconstmultiply=(a,b)=>a*bconstgetCalculator=()=>({ sum, multiply })wss.on('connection',ws=>{constclient=createNode()client.open(ws.send.bind(ws),getCalculator)ws.on('message',client.message)})// Clientconstws=newWebSocket('ws://localhost:8080')constserver=createNode()ws.on('open',async()=>{constgetCalculator=server.open(ws.send.bind(ws))const{ sum, multiply }=awaitgetCalculator()constresult1=awaitsum(5,5)constresult2=awaitmultiply(3,3)console.log(result1,result2)// 10, 9})ws.on('message',server.message)// Serverconst{ createStore }=require('dop')conststore=createStore({players: 0})functionsubscribeToServerStore(listener){// Incrementing number of player as a patchconstlisteners=store.applyPatch((state)=>{state.players+=1})// We emit the patch to all the subscriberslisteners.forEach(({ listener, patch })=>listener(patch))// Here we subscribe our clientstore.subscribe(listener)returnstore.state}// Clientconst{ createStore }=require('dop')// Getting the current state of the server and subscribing to itconststate=awaitsubscribeToServerStore(onPatch)// Creates a local store where our UX components can subscribe toconststore=createStore(state)functiononPatch(patch){// Applying patch from the serverconstlisteners=store.applyPatch(patch)// We emit the patch to subscribers. Like React components.listeners.forEach(({ listener, patch })=>listener(patch))}Check the website for more info https://distributedobjectprotocol.org/