https://github.com/mikeal/methodman
Bidirectional rpc and streams for WebSockets and WebRTC.
https://github.com/mikeal/methodman
Last synced: 2 months ago
JSON representation
Bidirectional rpc and streams for WebSockets and WebRTC.
- Host: GitHub
- URL: https://github.com/mikeal/methodman
- Owner: mikeal
- Created: 2016-09-28T17:46:17.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-11T21:56:02.000Z (almost 8 years ago)
- Last Synced: 2024-10-18T09:13:35.334Z (9 months ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 14
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Method Man
Bidirectional rpc and streams for WebSockets and WebRTC.
## Usage
basic
```javascript
const methodman = require('methodman')
const fs = require('fs')
const meth = methodman(duplexStream)
meth.commands({echo: (txt, cb) => cb(null, txt), ping: cb => cb(null)})
meth.on('commands', remote => {
if (remote.ping) {
let start = Date.now()
remote.ping(() => {
console.log(`pingtime ${Date.now() - start}`)
})
}
})
fs.createReadStream('test.file').pipe(meth.stream('test.file'))
meth.on('stream', (stream, id) {
stream.pipe(fs.createWriteStream(`${__dirname}/${id}`))
})
```### w/ WebSockets
client.js
```javascript
const websocket = require('websocket-stream')
const methodman = require('methodman')
const ws = websocket('ws://localhost:8080')
const meth = methodman(ws)
meth.commands({echo: (txt, cb) => cb(null, txt), ping: cb => cb(null)})
meth.on('commands', remote => {
// call remote commands
})
let substream = meth.stream()
// now i can take someStream and .pipe(substream)
meth.on('stream', (stream, id) => {
// I'm being sent a stream.
})
```server.js
```javascript
const websocket = require('websocket-stream')
const methodman = require('methodman')
function onWebsocketStream (stream) {
var meth = methodman(stream)
meth.commands({echo: (txt, cb) => cb(null, txt), ping: cb => cb(null)})
meth.on('commands', remote => {
// call remote commands
})
let substream = meth.stream()
// now i can take someStream and .pipe(substream)
meth.on('stream', (stream, id) => {
// I'm being sent a stream.
})
}
const wss = websocket.createServer({server: app}, onWebsocketStream)
```### w/ WebRTC
```javascript
const SimplePeer = require('simple-peer')
```### `methodman(stream)`
Takes a duplex stream to sit on top of.
Returns a MethodMan instance.
### `meth.stream([id])`
Returns a new writable substream.
### `meth.commands(rpc)`
Expose a set of commands defined in the rpc object.
Uses `dnode` under the hood.
### Event: "commands" (remote, id)
A set of commands you can call that have been exposed by the other end.
### Event: "commands:id" (remote)
A set of commands you can call that have been exposed by the other end.
### Event: "stream" (stream, id)
A readable stream being sent from the remote end.
### Event: "stream:id" (stream)
A readable stream being sent from the remote end.