Giter Site home page Giter Site logo

distributedobjectprotocol / dop Goto Github PK

View Code? Open in Web Editor NEW
170.0 9.0 10.0 2.65 MB

JavaScript implementation for Distributed Object Protocol

Home Page: https://distributedobjectprotocol.org

License: MIT License

JavaScript 100.00%
rpc protocol realtime distributed patches javascript real-time state-management data-synchronization json data

dop's Introduction

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.

Quick example using RPCs with WebSockets

// Server
const { createNode } = require('dop')
const WebSocket = require('ws')
const wss = new WebSocket.Server({ port: 8080 })

const sum = (a, b) => a + b
const multiply = (a, b) => a * b
const getCalculator = () => ({ sum, multiply })

wss.on('connection', ws => {
    const client = createNode()
    client.open(ws.send.bind(ws), getCalculator)
    ws.on('message', client.message)
})
// Client
const ws = new WebSocket('ws://localhost:8080')
const server = createNode()
ws.on('open', async () => {
    const getCalculator = server.open(ws.send.bind(ws))
    const { sum, multiply } = await getCalculator()
    const result1 = await sum(5, 5)
    const result2 = await multiply(3, 3)
    console.log(result1, result2) // 10, 9
})
ws.on('message', server.message)

Quick example using Stores and Patches

// Server
const { createStore } = require('dop')

const store = createStore({ players: 0 })

function subscribeToServerStore(listener) {
    // Incrementing number of player as a patch
    const listeners = store.applyPatch((state) => {
        state.players += 1
    })
    // We emit the patch to all the subscribers
    listeners.forEach(({ listener, patch }) => listener(patch))
    // Here we subscribe our client
    store.subscribe(listener)
    return store.state
}
// Client
const { createStore } = require('dop')

// Getting the current state of the server and subscribing to it
const state = await subscribeToServerStore(onPatch)
// Creates a local store where our UX components can subscribe to
const store = createStore(state)

function onPatch(patch) {
    // Applying patch from the server
    const listeners = 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/

License

MIT

dop's People

Contributors

gitter-badger avatar hugofqueiros avatar josema avatar satyam10zs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dop's Issues

Is this an active project?

I am wondering as the last commit was quite a while ago, and I have a use case for this, and need to know whether this project was actively maintained or not.

Issue on client with remote methods

Awesome library. Thanks for all your work!

I'm having an issue on the client having to do with the "path" variable being null/undefined when configuring remote methods.

Uncaught TypeError: Cannot read property '0' of undefined
at Object.dop.core.configureObject (dop.js:2056)
at Object.dop.core.configureObject (dop.js:2063)
at Object.dop.register (dop.js:1044)
at Object.dop.protocol._onsubscribe (dop.js:3386)
at Object.dop.core.emitMessage (dop.js:1247)
at WebSocket.onmessage (dop.js:259)

allow resubscribing to an object

Hi and thanks for this awesome library!! :)
I'm doing my home automation with it and it works pretty well so far.

But I encountered a situation where I'd like to periodically unsubscribe and resubscribe to an object, and it does not seem to work currently. It throws the following error when doing it: TypeError: dop.data.object[node.owner[object_owner_id]] is undefined

example client

var dop = require("dop");
var client = dop.connect({url:'ws://'+location.hostname+':4444'});
client.on("connect",async ()=>{
	var x = await client.subscribe("x");
	await client.unsubscribe(x);
	x = await client.subscribe("x");
})

example server

var dop = require("dop");
var x = dop.register({});
dop.onSubscribe(name=>x);
dop.listen();

What's interesting though, is that I don't get the error when always creating a new object, like so:

var dop = require("dop");
dop.onSubscribe(name=>dop.register({}));
dop.listen();

Does unsubscribe destroy the object on the server or something?

Mutations for connected client(s) are slow with more dop registered objects(>=2000)!!

Hi,

DOP library is written very well. I am using it in one of my projects.
Could you please suggest/answer my queries below.

My Scenario:

Lets say DOP server has 2000 objects registered and 5-10 DOP clients have subscribed to those 2000 objects to receive mutations.
The 2000 objects are updated every 500 milliseconds or below. With this configuration memory keeps increasing like more than 700MB+... so on and dop server goes down due to more memory consumption.

<--- Last few GCs --->

[7664:004209B0] 106816266 ms: Mark-sweep 680.7 (712.2) -> 680.7 (712.2) MB, 1905
.6 / 0.0 ms allocation failure GC in old space requested
[7664:004209B0] 106818103 ms: Mark-sweep 680.7 (712.2) -> 680.7 (710.2) MB, 1837
.1 / 0.0 ms last resort GC in old space requested
[7664:004209B0] 106819978 ms: Mark-sweep 680.7 (710.2) -> 680.7 (710.2) MB, 1875
.3 / 0.0 ms last resort GC in old space requested

I modified following sections of library to achieve the result.

  1. dop.core.sendMessages : here itself i called dop.protocol._onpatch(node, request_id, node.requests[request_id], response); and commented the timer part to avoid resending of data via new createrequest.
  2. Fixed the maximum limit for requests for every connected node. This will not send every mutation to connected client.
    dop.core.emitNodes = function (patch) {
    for..
    for..
    if(Object.keys(node.requests).length > 10000) continue;
    3.ws.js
    function send(message) {
    if (socket.readyState === OPEN) {
    socket.send(message);
    } //no queue
    }

Query??

The mutations for connected client will be slow as long as new client keeps on connecting and subscribing the 2000 dop registered obejcts. Is there any way, i can increase the client limit with smooth and fast updates at their end without lag?. Now 10-15 client works ok with my approach mentioned above .

Regards,
Satyam Singh

Roadmap.

Just wanted to say that this is an excellent little library, I will be implementing this into my personal projects as well as projects for work. Quick question, what's the roadmap like on this? Will there be inter-language support?

Contributing?

Hey guys! Love this lib and would like to help make some additions. Not finding any contributing guidelines. Is this project open to new people joining to help?

Server-to-Server communication using DOP.

Around a year ago, DOP provided Server-to-Server communication. I'm noticing now, however, the documentation is rather explicit on Server-to-Browser based communication. Is the legacy functionality still supported?

Thanks!

example for connecting with nodejs http server

is there a simple example for https://distributedobjectprotocol.org/guide/javascript/connecting-nodes with nodejs http server and web-browser?

i tried this:

const Fs = require("fs");
const Dop = require("dop");
const Http = require("http");

const httpd = Http.createServer((request, response)=>{
	switch (request.url) {
		case "/":
			response.writeHead(200, {"Content-Type":"text/html"});
			response.end(`
				<html>
				<head>
					<script src="/dop.js"></script>
					<script>
						const server = dop.connect();
						server.on("connect", ()=>{
							console.log("connected");
						});
					</script>
				</head>
				<body><h1>hello world</h1></body>
				</html>
			`);
			break;
		case "/dop.js":
			response.writeHead(200, {"Content-Type":"application/javascript"});
			Fs.createReadStream("./node_modules/dop/dist/dop.js").pipe(response);
			break;
		default:
			response.writeHead(404, {"Content-Type":"text/plain"});
			response.end("not found.");
			break;
	}
});

const dopd = Dop.listen({httpServer:httpd});
dopd.on("connect", (node)=>{
	console.log("connected");
});

httpd.listen(8000, "localhost");

and i only get "connected" printed in web-browser devtools console (chromium 69.0.3497.81), but not in nodejs (10.10.0) console.

what am i doing wrong?

ps. i could not find in dop documentation how to do web socket upgrades (or even what options does .listen() use), but found .listen({httpServer:httpd}) in some other example, in the hopes it does that...

Btc not credited to coinfy wallet

The coinfy repository has been archieved by Josema the same creator for this App, i am not able to reply to your message because you have archieved the repository, i need help with the btc deposit which is still missing from the Coinfy wallet, i followed your instructions and exported the 12 keys to monedero wallet however there is still no funds in it. How do i go about getting my funds from my coinfy wallet, the blockchain have 121 confirmations for the transactions but my btc wallet still have no funds in it even after exporting the wallet to monedero.

How to implement Client reconnect?

Hi,

my client (browser) does not reconnect when I restart the server. Neither using the default ws (kind of expected) nor when using Socket.IO (not expected) ... is that normal behaviour?

Thanks,
Jan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.