开发者

Can I connect directly to a Redis server from JavaScript running in a browser?

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?


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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜