0.0.3 • Published 7 years ago
voodoo-client v0.0.3
voodoo-client
The voodoo client is used to "communicate" with the voodoo-server over whatever transport you choose to use.
Creating the client
const Client = require('voodoo-client')
const client = new Client()
Done. But now you have to add a transport to be able to do anything else then logging in.
client.transport(function)
Plugins
There are few included transport plugins:
tcp
The tcp transport takes the same arguments as net.connect
const tcptransport = require('voodoo-server/plugins/tcp-transport')
vo.transport(tcptransport({
path: '/tmp/voodoo.sock'
}))
websocket
The websocket transport is based on engine.io-client takes the same arguments as engine.io-client
const wstransport = require('voodoo-server/plugins/ws-transport')
vo.transport(wstransport('ws://localhost:6344'))
client.login({credentials})
Login return a axios Promise
and automatically store the token for future requests over the given transport.
client.login({
username: 'johano'
})
client.define(name, handler)
The send command can be sent as many times as you like until you choose to end the subject.
client.define('say-hello', (req, res) => {
for (let i = 0; i <= req.data.times || 1; i += 1)
res.send(`Hello ${req.user.name}!!!`)
res.end()
})
- res.send({message}) send the given message
- res.error({error}) send the given error
- res.end({message}) send the given message(optional) and end the rpc stream
client.run(name, {data}, callback)
client.run('say-hello', {
times: 3
}, (data) => {
console.log(data) // Hello Johan Olsson!!!
// Hello Johan Olsson!!!
// Hello Johan Olsson!!!
})
client.subscribe(name, handler)
client.subscribe('say', (event) => {
console.log(`${event.user.name} said ${event.data.message}`) // Johan Olsson said Hi!!!
})
client.emit(name, {data})
client.emit('say', {
message: 'Hi!!!'
})