Linux 使用(客户端)javascript 直接连接到 Redis?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6311984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting directly to Redis with (client side) javascript?
提问by MichaelMackus
Is there a way to directly connect to Redis using client side (not Node.js) javascript?
有没有办法使用客户端(不是 Node.js)javascript 直接连接到 Redis?
I'm already using Node.js + PHP + Redis + Socket.io (for the client) successfully for a few projects. However, I really think this could be further simplified to something like PHP + Redis + Browser javascript - taking out the Node.js server which is just another server I'd rather not use if it isn't necessary. For simple things, I think it would be better to just connect directly to Redis using Javascript.
我已经成功地将 Node.js + PHP + Redis + Socket.io(用于客户端)用于一些项目。但是,我真的认为这可以进一步简化为 PHP + Redis + Browser javascript 之类的东西——去掉 Node.js 服务器,它只是另一个服务器,如果没有必要,我宁愿不使用它。对于简单的事情,我认为使用 Javascript 直接连接到 Redis 会更好。
From what I understand, Redis just serves its request through a port so any language that can make requests to that port would work. In theory, couldn't you just hit the redis server's port using client side javascript?
据我所知,Redis 只是通过一个端口来处理它的请求,因此任何可以向该端口发出请求的语言都可以工作。从理论上讲,您不能使用客户端 javascript 访问 redis 服务器的端口吗?
I'm mostly interested in the publish/subscribe functions, which may or may not be possible.
我最感兴趣的是发布/订阅功能,这可能也可能不可能。
I'm not sure if you can access a non-port 80 port using AJAX, but you technically should be able to forward Redis' port to port 80 using Nginx reverse proxy or something.
我不确定您是否可以使用 AJAX 访问非端口 80 端口,但从技术上讲,您应该能够使用 Nginx 反向代理或其他方式将 Redis 的端口转发到端口 80。
Any ideas? Just a thought. I'm very happy with my current solution, but it doesn't hurt to wonder if we could do this even better or more efficiently.
有任何想法吗?只是一个想法。我对我目前的解决方案非常满意,但不知道我们是否可以做得更好或更有效。
采纳答案by Jordan Running
You can only make HTTP requests with client-side JavaScript and, in some browsers, websockets. However, you should look into Webdis. It adds an easy HTTP/JSON layer to Redis and should do exactly what you want.
您只能使用客户端 JavaScript 发出 HTTP 请求,在某些浏览器中,还可以使用 websocket。但是,您应该查看Webdis。它为 Redis 添加了一个简单的 HTTP/JSON 层,并且应该完全符合您的要求。
Edit:Link fixed.
编辑:链接已修复。
回答by tborg
I have a bunch of predefined redis accessors in php, and I use a 'router' style function to use them from the client via $.post requests with jQuery. The router is just a big switch:
我在 php 中有一堆预定义的 redis 访问器,我使用“路由器”样式函数通过带有 jQuery 的 $.post 请求从客户端使用它们。路由器只是一个大开关:
public function router() {
$response = array();
switch ($_POST['method']) {
case 'get_whole_list': //is a convenience function with arg $list_key
if ($_POST['list_key']) {//which will be provided by the POST request data
$response = $this->get_whole_list($_POST['list_key']);
break;
} else {
$response = (array('error' => 'must be passed with post key "list_key"'));
break;
} //and so on, until
//it's time to send the response:
return json_encode(array('response' => $response));
}
and then you just echo $myClass->router()
然后你就 echo $myClass->router()
I access it with jQuery like:
我使用 jQuery 访问它,例如:
redgets.get_whole_list = function(key, callback) {
$.post(redgets.router, //points to my php file
{method: 'get_whole_list', //tells it what to do
list_key: key}, //provides the required args
function(data) {
callback($.parseJSON(data).response); //parses the response
});
this all works fine; maybe it's not ideal, but it does make a node.js server redundant. I am surprised that nobody has already made a general-purpose redis interface in this style.
这一切正常;也许它并不理想,但它确实使 node.js 服务器变得多余。我很惊讶没有人已经制作了这种风格的通用 redis 接口。
回答by Maurice
The real obstacle is overcoming the non-port 80/443 limitation for the ajax request in the browser; Even with the Webdis solution, because it runs off port 7379 by defaul,t and would conflict with your Apache or Nginx process if ran off port 80.
真正的障碍是克服浏览器中ajax请求的非80/443端口限制;即使使用 Webdis 解决方案,因为它默认在端口 7379 上运行,并且如果在端口 80 上运行会与您的 Apache 或 Nginx 进程发生冲突。
My advice would be to use the nginx proxy_pass to point to webdis process. You can redirect traffic to port 80 and perform ajax request without the annoying security issues.
我的建议是使用 nginx proxy_pass 指向 webdis 进程。您可以将流量重定向到端口 80 并执行 ajax 请求,而不会出现烦人的安全问题。
Below is a sample NGINX configuration that seems to do the trick for me.
下面是一个示例 NGINX 配置,它似乎对我有用。
upstream WebdisServerPool
{
server 127.0.0.1:7379; #webdis server1
server 192.168.1.1:7379; #webdis server 2
}
server {
listen 80; #
root /path/to/my/php/code/;
index index.php;
server_name yourServerName.com;
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/my/php/code/$fastcgi_script_name;
}
location /redis {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
rewrite /(.*)/(.*)/(.*)$ // break; #ignore the /redis
proxy_redirect off;
proxy_pass http://webdisServerPool;
}
}
On the front end side, here is an example of getting all the keys. All redis requests would go through /redis for example:
在前端,这是获取所有密钥的示例。所有 redis 请求都会通过 /redis 例如:
$.ajax({
url: "/redis/KEYS/*",
method: 'GET',
dataType: 'json',
success:function(data)
{
$each(data.KEYS,function(key,value){
$('body').append(key+"=>"+value+" <br> ");
});
}
});
OR
或者
You could use:
你可以使用:
http://wiki.nginx.org/HttpRedisand parse the response yourself.
http://wiki.nginx.org/HttpRedis并自己解析响应。
回答by b1c9g76
I have found that the direct Redis http interfaces don't work very well with pub/sub or are difficult to set up (at time of writing).
我发现直接 Redis http 接口在 pub/sub 中不能很好地工作,或者很难设置(在撰写本文时)。
Here is my "workaround" for pub/sub based on the predis examples.
这是我基于 predis 示例的 pub/sub 的“解决方法”。