Skip to content

Redis HTTP interface and websocket messaging Node.js server

Notifications You must be signed in to change notification settings

b-goodman/redis-websocket-host

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Redis Websocket Host

A rudimentery websocket server which combines socket.io with redis to form a redis HTTP interface, and realtime messenger.

Requires an existing installation (see either redis or homebrew )

Redis HTTP Interface

Append any redis-cli query to the base URL, seperated by back-slashes.

E.g,

Show all keys

GET /keys/*

{"args":["keys","*"],"response":[["search-cache","latest_activity","status","anotherkey"]]}

Set a key test to value hello

GET /set/test/hello

{"args":["set","test","hello"],"response":["OK"]}

Query the value of test

GET /get/test

{"args":["get","test"],"response":["hello"]}

Delete the key test

GET /del/test

{"args":["del","test"],"response":[1]}

Websocket Integration

Any redis key modified via the HTTP API may also be listened to via a websocket inteface.

Node.js Client

//The URL is the API base plus the key you want to listen to: redis-key-<key name>
const url = '/redis-key-AN_EXAMPLE_KEY';
var socket = require('socket.io-client')(url);

socket.emit("new_client", window.location.href);
socket.on("new_client", (msg) => {
    console.log(msg);
});

//an update to the key emits a "local_redis_update" event.
socket.on("local_redis_update", (msg)=>{
    //Do something when a message comse through
    console.log(msg);
})

Browser Client

let socket = null;
$(function () {
    socket = io('/redis-key-demonstrations_build_status');
    socket.emit("new_client", window.location.origin);
    socket.on("new_client", (msg) => {
        console.log(msg)
    });
});

socket.on("local_redis_update", (resp) => {
    //Do something when a message comes through.
    //For example, this updates a progress bar.
    if(resp.key == jobTriggerID){
        const print_response = (resp.p_max == resp.progress) ? `Complete.  Files saved to ${resp.msg}` : `${resp.msg} category ${resp.progress} of ${resp.p_max}`;
        console.log(print_response);
        setProgress( parseInt(resp.progress), parseInt(resp.p_max) );
    }
});

About

Redis HTTP interface and websocket messaging Node.js server

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published