I know there are node.js libraries for Redis; what I'd like to do is run a Redis server (either on localhost or on a server host somewhere) and call it directly via HTTP (i.e. AJAX or HTTP GET as needed) from JavaScript running inside a browser (i.e. a Greasemonkey or Chrome Extension script, or maybe a bookmarklet or SCRIPT tag). Does Redis have a native REST or HTTP API?
-
I'm considering asking this question again, but stipulating we want a 'real' as in realtime redis-client -- not HTTP anything -- operating in the browser. Could build a great realtime 'infrastructure' with just CDN serving assets constituting the client webapp communicating with Redis directly. I want to cut out the unnecessary WebSocket server aspect of the system. All the control logic can be internalised to redis cluster in Lua. – Wylie Kulik Oct 20 '15 at 15:07
-
http://stackoverflow.com/questions/33241247/what-would-it-take-to-implement-a-good-redis-client-in-the-web-browser – Wylie Kulik Oct 20 '15 at 15:54
4 Answers
You can't connect directly to Redis from JavaScript running in a browser because Redis does not speak HTTP. What you can do is put webdis in front of Redis, it makes it possible work with a Redis instance over a HTTP interface.

- 6,709
- 1
- 25
- 36

- 131,503
- 21
- 160
- 205
-
Currently there is no Windows implementation: https://github.com/nicolasff/webdis/issues/138 – icc97 Mar 10 '17 at 13:40
You can literally connect to the redis server over http, and there's a security exploit based on this.
Redis is effectively a HTTP server -- http://benmmurphy.github.io/blog/2015/06/04/redis-eval-lua-sandbox-escape/
Maybe this could be used to make a javascript client for redis? In the examples shown, commands are sent directly to the redis server, which executes them. However, practically speaking, you can use openresty+nginx in front of redis to essentially directly talk to redis over http, and get the performance benefit of nginx's multithreaded server which will share a limited set of connections to redis itself.

- 643
- 4
- 10
As @Theo explained, you can't connect directly, but if you have webdis and redis set up then I have a library that eliminates mucking around with ajax yourself, in favor of a promises based approach.
webdismay is a JS library I have recently released (License: MIT) to connect to a webdis+redis backend from the browser. It takes an ES6 Promises approach to communicating with the redis+webdis back end, providing functions for generic and keyless redis commands and organized classes for commands that operate on Keys/Strings, Lists, Hash and Sets. All functions/methods return ES6 Promises.
Assuming you have webdis set up with redis, in the default configuration to accept post requests to "/", then with webdismay a simple example of sending data to the server and getting it back later would look like this on the browser (in ES6):
import 'whatwg-fetch'; // fetch polyfill
import * as W from 'webdismay';
const k = W.key('some-redis-key');
k.set('Hello, World!'); // store the information
// wait a while, e.g. in the dev console or with setTimeout()
k.get().then((v)=>console.log(v)); // --> Hello, World!
At the time I am writing this (July 2016), the first two import
lines require some translation and packaging assistance from tools like jspm or browserify (if you change the import
to require
).
This is not exactly beginner-friendly, yet, but could allow someone to use webdis+redis from the browser without constantly translating mentally between javascript idioms and redis and writing their own ajax.

- 26,170
- 12
- 85
- 119
Webdis is very very slow as compared to using an NGINX server in front of redis
If you just implement a simple redis client in mod-perl and expose behind nginx, you can easily get very good performance. And you can handle a lot of logic too

- 1,155
- 13
- 34
-
6As the author of Webdis and having done *many* benchmarks of Webdis and alternatives, I strongly disagree with this statement. Webdis is multi-threaded and easily able to saturate a single-threaded Redis instance which quickly becomes the bottleneck in a benchmark. When I was developing Webdis I had discussions with the author of the [Redis nginx module](https://github.com/openresty/redis2-nginx-module) and we both came to the conclusion that Webdis was faster. Additionally, the suggestion to use mod-perl is highly unlikely to lead to better performance than using Webdis – quite the opposite. – Nicolas Favre-Felix Feb 25 '21 at 05:43
-
1This doesn't appear to be an answer to the original question ("Can I connect directly to a Redis server from JavaScript running in a browser?" / "Does Redis have a native REST or HTTP API?") but rather a response to another answer. As such, it is better suited as a comment on that answer. – user513951 Jan 03 '23 at 12:22