redisjs
library is a redis client for javascript client side. It uses websocket with a bridge server to proxy the websocket connections to tcp socket connections (i.e caddy wsproxy)
npm install https://github.com/incubaid/redisjs
Then you can import RedisConnection into your js files:
import RedisConnection from 'redisjs';
Include the minified script from the build directory into your html file
<script src="build/redisjs.min.js"></script>
- Create RedisConnection to the caddyserver which acts as websocket proxy
const SERVER_DOMAIN = 'localhost:8200';
const redisConnection = new RedisConnection(`${SERVER_DOMAIN}`);
const successCallback = (conn) => {
// Run your redis commands here in the success callback
};
const errCallback = (err) => {
console.log(err);
};
redisConnection.connect(successCallback, errCallback);
- Run gedis commands using methods similar to redis-commands names and arguments. It follows the format of '{namespace}:{command}'.
conn['system.set']('myNumbers', [1, 2, 3], (res) => {
console.log(res);
}, (err) => {
console.log(`Error: ${err.message}`);
});
conn['system.set']('myNumbers', (res) => {
console.log(res);
}, (err) => {
console.log(`Error: ${err.message}`);
});
- Run redis commands using methods with the same redis-commands names and arguments
// Inside the connection success callback run your commands
conn.set('myNumbers', [1, 2, 3], (res) => {
console.log(res);
}, (err) => {
console.log(`Error: ${err.message}`);
});
conn.get('myNumbers', (res) => {
console.log(res);
}, (err) => {
console.log(`Error: ${err.message}`);
});
conn.INVALID_CMD('myNumbers', (res) => {
console.log(res);
}, (err) => {
console.log(`Error: ${err.message}`);
});