Toa-net
JSON-RPC 2.0 client/server over TCP net.
Features
- Use JSON-RPC 2.0 Specification as RPC protocol.
- Use RESP (Redis Serialization Protocol) or MsgP (Byte Message Protocol) as message protocol.
- Use JSON Web Signatures as authentication protocol.
- Implemented ES6 Iterable protocol.
Implementations
- snapper-core Teambition push messaging service, based on redis.
- snapper-producer Snapper producer client for node.js.
Examples
Simple
const net = const auth = 'secretxxx'const server = { socket}server // Enable authentication for serverserver { return auth} const client = // Enable authentication for clientclient { return auth}client clientclientclientclient { console // null { a: 4 } client server}
Iterator
Socket is async iterable object!
const thunk = const net = // 创建服务器const server = { { console process }}server // 创建客户端const client = // 向服务器发出 notificationclientclientclient// 向服务器发出 RPC 请求,服务器将 echo 请求数据client { console // null { a: 4 } client server}
https://github.com/toajs/toa-net/tree/master/bench
BenchgRPC vs axon vs toa-net, 5000000 Ping/Pong messages, 1 TCP connection, Node.js v6
- gRPC, no-delay: 1000 cocurrency, 1240066 ms, 4032.04 ops
- axon, no-delay: 1000 cocurrency, 204176 ms, 148888.89 kb, 24488.68 ops
- toa-net, no-delay: 1000 cocurrency, 73789 ms, 263272.57 kb, 67760.78 ops
100000 Ping/Pong messages
- local -> local, no-delay: 1000 cocurrency, 3180ms, 31446 ops
- local -> local, delay 1000ms: 1000 cocurrency, 100590ms, 994 ops
- local -> local, delay 1000ms: 5000 cocurrency, 20869ms, 4791 ops
- local -> local, delay 1000ms: 10000 cocurrency, 11074ms, 9030 ops
10000 simple messages, 1000 cocurrency
// message name: 'abcdefghijklmnopqrst' email: 'abcdefghijklmnopqrst@test.com' location: 'zhangjiang, shanghai, china'
- aliyun -> aws: 264321ms, 37 ops, 4.61 kb/s
- aws -> aliyun: 82129ms, 121 ops, 14.84 kb/s
- aliyun -> proxy_cn -> fiber -> proxy_us -> aws: 8056ms, 1241 ops, 151.30 kb/s
Install
npm install toa-net
API
const toaNet =
Class toaNet.Server
new toaNet.Server(connectionListener)
Create RPC server.
const server = { socket}server
connectionListener
: Required, Type:Function
.
Event: 'close'
Event: 'error'
Event: 'listening'
server.getAuthenticator()
Abstract method. Should be overridden to enable authentication.
Default:
server { return null // Disable authentication}
Enable authentication:
const auth = 'secretxxx' server { return auth}
server.address()
Returns the bound address.
server.connections: RingPool
server.connections.length
Returns the number of concurrent connections on the server.
server.connections.next()
Return a socket in turn. Return null
if no socket available.
server.close([callback])
Closes the server.
server.listen(...)
Same as node.js server.listen
Class toaNet.Client
Event: 'close'
Event: 'connect'
Event: 'auth'
Event: 'message'
Event: 'drain'
Event: 'end'
Event: 'error'
Event: 'timeout'
new toaNet.Client([options])
Creates RPC client.
const client =
-
options.retryDelay
: Optional, Type:Number
, Default:500
ms. Sets time interval for reconnection. -
options.maxAttempts
: Optional, Type:Number
, Default:50
. Sets max attempts for reconnection. -
options.tcpTimeout
: Optional, Type:Number
, Default:0
. Sets the socket to timeout after timeout milliseconds of inactivity on the socket. -
options.tcpNoDelay
: Optional, Type:Boolean
, Default:true
. Disables the Nagle algorithm. -
options.tcpKeepAlive
: Optional, Type:Boolean
, Default:true
. Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket.
client.connect(...)
Same as node.js socket.connect
client
client.getSignature()
Abstract method. Should be overridden to enable authentication.
Default:
client { return '' // Disable authentication}
Enable authentication:
const auth = 'secretxxx' client { return auth}
client.request(method[, params])
Creates a JSON-RPC 2.0 request to another side. Returns thunk function.
client { console}
method
: Required, Type:String
.params
: Optional, Type:Object|Array
.
client.notification(method[, params])
Creates a JSON-RPC 2.0 notification to another side. No return.
client
method
: Required, Type:String
.params
: Optional, Type:Object|Array
.
client.success(id, result)
Respond success result to the request of id
. No return.
client
id
: Required, Type:String|Integer
, the request'sid
.result
: Required, Type:Mixed
.
client.error(id, error)
Respond error to the request of id
. No return.
client
id
: Required, Type:String|Integer
, the request'sid
.error
: Required, Type:Error
.
client.createError(error[, code, data])
client.createError(message[, code, data])
client.createError(code[, data])
client.throw(error[, code, data])
client.throw(message[, code, data])
client.throw(code[, data])
client.handleJsonRpc(jsonRpc, handleFn)
client.address()
client.destroy()
client[Symbol.iterator]()
Class toaNet.Auth
new toaNet.Auth(options)
Creates auth object for Server and Client.
const auth = expiresIn: 3600 secrets: 'secretxxx1' 'secretxxx2' 'secretxxx3'
options.secrets
: Required, Type:String
or aArray
of string.options.expiresIn
: Optional, Type:Number
, Default:3600
seconds.options.algorithm
: Optional, Type:String
, Default:'HS256'
.
auth.sign(payload)
Returns a new signature string.
let signature = auth
auth.verify(signature)
Verify the signature, return payload object if success, or throw a error.
let session = auth
auth.decode(signature)
Try decode the signature, return payload object if success, or null
.
let signature = auth
Advance API
Class toaNet.Resp
Class toaNet.Queue
Class toaNet.Socket
Class toaNet.RingPool
Class toaNet.RPCCommand
toaNet.jsonrpc
License
Toa-net is licensed under the MIT license. Copyright © 2016-2018 Toajs.