|
| 1 | +<div align="center"> |
1 | 2 |
|
2 | | -<p align="center"><a href="https://distributedobjectprotocol.org"><img width="200"src="https://distributedobjectprotocol.org/img/logo.svg"></a></p> |
| 3 | +<p align="center"><a href="https://distributedobjectprotocol.org"><img src="https://distributedobjectprotocol.org/img/logo.svg" width="200"></a></p> |
3 | 4 |
|
4 | | -<p align="center"> |
5 | | - <a href="https://www.npmjs.com/package/dop"><img alt="npm version" src="https://img.shields.io/npm/v/dop.svg"></a> |
6 | | - <a href="https://travis-ci.org/DistributedObjectProtocol/dop"><img alt="Build Status" src="https://api.travis-ci.org/DistributedObjectProtocol/dop.svg?branch=master"></a> |
7 | | - <a href="https://www.npmjs.com/package/dop"><img alt="license" src="https://img.shields.io/npm/l/dop.svg"></a> |
8 | | - <a href="https://spectrum.chat/dop"><img alt="Join the community" src="https://withspectrum.github.io/badge/badge.svg"></a> |
9 | | -</p> |
| 5 | +[](https://www.npmjs.com/package/dop) |
| 6 | +[](https://bundlephobia.com/result?p=dop) |
| 7 | +[](https://travis-ci.org/DistributedObjectProtocol/dop) |
| 8 | + |
10 | 9 |
|
11 | | -## Distributed Object Protocol is for |
| 10 | +<br/> |
| 11 | +<a href="https://distributedobjectprotocol.org/guide/javascript">https://distributedobjectprotocol.org/</a> |
| 12 | +<br/> |
| 13 | +<br/> |
12 | 14 |
|
13 | | -**State management**, Remote procedure calls, Reactive programming, |
14 | | -Data sync, Pub/Sub, Optimistic updates, Time-travel debugging, Unidirectional data flow and **Real time apps**. |
| 15 | +</div> |
15 | 16 |
|
16 | | -This repository is the JavaScript implementation of the protocol that runs on node.js and Browsers. |
| 17 | +**Distributed Object Protocol** is a thin layer on top of your data network that helps you communicate server and clients (nodes) using [RPCs](https://en.wikipedia.org/wiki/Remote_procedure_call). It is also a pattern that makes easy update, mutate or even sync the state of your App using [Patches](https://github.com/DistributedObjectProtocol/protocol#Patches). |
17 | 18 |
|
18 | | -<!-- |
19 | | -## Connecting two nodes |
| 19 | +## Quick example using RPCs with WebSockets |
20 | 20 |
|
21 | 21 | ```js |
22 | | -// Server (node.js) |
23 | | -const dop = require('dop') |
24 | | -const object = dop.register({ |
25 | | - fullname: 'John Doe', |
26 | | - square: number => number * number |
| 22 | +// Server |
| 23 | +const { createNode } = require('dop') |
| 24 | +const WebSocket = require('ws') |
| 25 | +const wss = new WebSocket.Server({ port: 8080 }) |
| 26 | + |
| 27 | +const sum = (a, b) => a + b |
| 28 | +const multiply = (a, b) => a * b |
| 29 | +const getCalculator = () => ({ sum, multiply }) |
| 30 | + |
| 31 | +wss.on('connection', ws => { |
| 32 | + const client = createNode() |
| 33 | + client.open(ws.send.bind(ws), getCalculator) |
| 34 | + ws.on('message', client.message) |
27 | 35 | }) |
28 | | -dop.listen() // WebSockets on port 4444 (https://github.com/websockets/ws) |
29 | | -dop.onSubscribe(() => object) |
30 | 36 | ``` |
31 | 37 |
|
32 | 38 | ```js |
33 | | -// Client (browser) |
34 | | -import dop from 'dop' |
35 | | -const server = dop.connect() // Native WebSockets 'ws://localhost:4444' |
36 | | -const objectFromServer = await server.subscribe() |
37 | | -console.log(objectFromServer.fullname) // > "John Doe" |
38 | | -console.log(await objectFromServer.square(5)) // > 25 |
39 | | -```--> |
40 | | - |
41 | | - |
42 | | - |
43 | | - |
44 | | - |
45 | | -Check the website for more detailed information [https://distributedobjectprotocol.org/](https://distributedobjectprotocol.org/) |
46 | | - |
| 39 | +// Client |
| 40 | +const ws = new WebSocket('ws://localhost:8080') |
| 41 | +const server = createNode() |
| 42 | +ws.on('open', async () => { |
| 43 | + const getCalculator = server.open(ws.send.bind(ws)) |
| 44 | + const { sum, multiply } = await getCalculator() |
| 45 | + const result1 = await sum(5, 5) |
| 46 | + const result2 = await multiply(3, 3) |
| 47 | + console.log(result1, result2) // 10, 9 |
| 48 | +}) |
| 49 | +ws.on('message', server.message) |
| 50 | +``` |
47 | 51 |
|
| 52 | +Check the website for more info [https://distributedobjectprotocol.org/](https://distributedobjectprotocol.org/guide/javascript) |
48 | 53 |
|
49 | 54 | ## License |
50 | 55 |
|
|
0 commit comments