vsock
v1.0.6
Published
Virtual socket system, the package provides the way to realtime connect between HTTP client and HTTP server without websocket
Downloads
14
Maintainers
Readme
VSock
A virtual socket system
VSock is stand for virtual socket, this library provides a way to real-time connect beetween client and server over http/https protocol. VSock uses ExpressJs to create 2 routes:
- POST /vsock/pull : wait a message from VSock
- POST /vsock/push : push a message to VSock
You must setup a Redis server before using. VSock uses Redis to store global variables: message id, temporary messages... VSock also uses Redis to scale systems by adding more servers when reaching to the resource limits. The library uses Subscribe/Publish feature to comunity beetween servers/nodes.
Usage
Server code
var express = require('express'),
VSock = require('vsock'),
app = express();
// start http server at port 80
app.listen(80, function () {
console.log(`Http's started at port 80`);
// init a vsock intance with options:
// redis: an object info to connect to redis see more at: https://github.com/NodeRedis/node_redis#options-object-properties
// readTimeout: the maximum time in milliseconds that a temporary variables is stored in redis
let vsock = new VSock({
redis: {
host: 'localhost',
port: 6379,
prefix: 'vsock:'
},
readTimeout: 10000
});
// set a function to property onconnect to receive callback each new incoming connect
// req: a request express object, you can check request data before accepting a connect
// accept: a function, must call if accept a connection
// reject: a function, must call if reject a connection
// you can ignore this section, all connections would be accepted
vsock.onconnect = function (req, accept, reject) {
if (req.body.token && req.body.token == '123') {
accept();
} else {
reject();
}
}
// set this property to receive callback for each push message
// req: a request express object, you can check data before accepting a message sending to room
// accept: a function, you must call this to accept message sending to room
// reject: a function, you must call this reject if you want to ignore message sending to room
// you can ignore this section, all incoming message would be accepted
vsock.onpush = function (req, accept, reject) {
if (req.body.token && req.body.token == '123') {
accept();
} else {
reject();
}
}
});
Client code