前边有写过一个简单pushpin 集成stream 的demo,这次测试下sse 的功能
备注: 环境依然使用的是docker-compose运行

环境准备

  • docker-compose 文件
version: "3"
services:
pushpin:
image: fanout/pushpin
environment:
- "target=api:8080"
- "LOGNAME=nobody"
volumes:
- "./routes:/etc/pushpin/routes"
ports:
- "5561:5561"
- "5562:5562"
- "5563:5563"
- "7999:7999"
api:
build: ./
ports:
- "8080:8080"
volumes:
- "./nginx_lua/:/opt/app/"
- "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
  • nginx.conf
worker_processes 1;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
lua_code_cache off;
lua_need_request_body on;
gzip on;
resolver 127.0.0.11 ipv6=off;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
lua_package_path '/opt/app/?.lua;;';
server {
listen 8080;
server_name app;
charset utf-8;
default_type text/html;
location / {
default_type text/plain;
index index.html index.htm;
}
location = /favicon.ico {
root /opt/app/static;
}
# sse 测试
location /sse {
content_by_lua_block {
require("api/sse")()
}
}
## stream 测试
location /userinfo {
content_by_lua_block {
require("api/userinfo")()
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} }
}
  • dockerfile
FROM openresty/openresty:alpine-fat
LABEL author="1141591465@qq.com"
RUN /usr/local/openresty/luajit/bin/luarocks install lua-resty-http
RUN opm get thibaultcha/lua-resty-jit-uuid

sse lua 代码

sse 代码和stream 类似,主要是content_type 以及content 内容格式
nginx_lua/api/sse.lua

local json = require("cjson")
local http = require("resty.http")
function init()
local uuid = require("resty.jit-uuid")
uuid.seed()
local method_name = ngx.req.get_method()
if method_name == "GET" then
ngx.header["Content-Type"] = "text/event-stream"
ngx.header["Grip-Hold"] = "stream"
ngx.header["Grip-Channel"] = "loginfo"
ngx.say("is stream open")
return
end
ngx.req.read_body()
local body = ngx.req.get_body_data()
if not body then
if ngx.req.get_body_file() then
return nil, "request body did not fit into client body buffer, consider raising 'client_body_buffer_size'"
else
return ""
end
end
local mode = json.decode(body)
-- event: update\ndata: hello world\n\n local body_entity = {
items = {
{
channel = mode.channel,
id = uuid(),
formats = {
["http-stream"] = {
content = mode.data,
},
-- action = "send"
}
}
}
}
ngx.log(ngx.ERR, json.encode(body_entity))
local requestapiurl = "http://pushpin:5561/publish/"
local httpc = http.new()
local res, err =
httpc:request_uri(
requestapiurl,
{
method = "POST",
body = json.encode(body_entity),
headers = {
["Content-Type"] = "application/json"
}
}
)
if not res then
ngx.say("failed to request: ", err)
return
end
ngx.log(ngx.ERR, res.body)
ngx.say(res.body)
end
return init

启动&&测试

  • 启动
docker-compose up -d
curl -X POST \
http://localhost:8080/sse \
-H 'Content-Type: application/json' \
-H 'Postman-Token: 0c28ab8b-1128-47cf-92b5-8cf6061dbff7' \
-H 'cache-control: no-cache' \
-d '{
"channel":"loginfo",
"data": "event: userlogin\ndata: demo login \n\n"
}'

效果

说明

pushpin 的功能还是很强大的,开发起来也比较简单,对于我们需要realtime api 的需求还是很方便的

参考资料

https://pushpin.org/docs/usage/#subscribing
https://github.com/rongfengliang/pushpin-openresty-docker-compose

 
 
 
 

pushpin Server-sent events && openresty 集成试用的更多相关文章

  1. pushpin openresty 集成试用

    pushpin 是一个很不错的将restapi 转换为reailtime api 的proxy,openresty 具有很强的nginx 控制能力 可以方便的用来进行api 的开发,默认其他语言pus ...

  2. gearman openresty 集成试用

    很简单使用了一个openresty 的lua 模块 环境准备 docker-compose 文件 详细配置可以参考 https://github.com/rongfengliang/gearmango ...

  3. Play Framework, Server Sent Events and Internet Explorer

    http://www.tuicool.com/articles/7jmE7r Next week I will be presenting at Scala Days . In my talk I w ...

  4. server sent events

    server sent events server push https://html5doctor.com/server-sent-events/ https://developer.mozilla ...

  5. openresty 集成 keycloak-oauth-oidc

    keycloak 是一个比较全,而且比较方便的sso 解决方案,同时为我们提供了灵活的扩展特性 备注: 测试使用docker-compose 运行,对于keycloak 使用pg 数据库做为后端存储 ...

  6. openresty 集成 sentry 异常系统

    sentry 是一个方便的错误异常追踪系统,同时社区也提供了openresty 的lua 包,使用docker-compose 进行测试 备注: sentry 部分的配置来自官方文档 环境准备 doc ...

  7. openresty 集成lua-resty-mail +smtp2http 扩展灵活的mail 服务

    lua-resty-mail 是一个不错的openresty mail 扩展,我们可以用来进行邮件发送,支持附件功能 smtp2http 是一个smtp 服务,可以将smtp 请求数据转换为http ...

  8. SQL Server Extended Events 进阶 2:使用UI创建基本的事件会话

    第一阶中我们描述了如何在Profiler中自定义一个Trace,并且让它运行在服务器端来创建一个Trace文件.然后我们通过Jonathan Kehayias的 sp_SQLskills_Conver ...

  9. SQL Server Extended Events 进阶 1:从SQL Trace 到Extended Events

    http://www.sqlservercentral.com/articles/Stairway+Series/134869/ SQL server 2008 中引入了Extended Events ...

随机推荐

  1. 3.1 Makefile

    安装make 安装make sudo apt-get install make make -v

  2. x多进程

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' from multiprocessing import Process import os #子进 ...

  3. SQL-13 从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。

    题目描述 从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t.CREATE TABLE IF NOT EXISTS "titles" ( ...

  4. Centos7安装ansible

    CentOS下部署Ansible自动化工具 1.确保机器上安装的是 Python 2.6 或者 Python 2.7 版本: python -V 2.查看yum仓库中是否存在ansible的rpm包 ...

  5. 自动化测试工具Telerik Test Studio发布R1 2019|附下载

    Telerik Test Studio是一个用于功能性Web.桌面和移动测试的直观测试自动化工具,它能轻松地实现自动化测试.同时会为GUI.性能.加载和API测试提供完整的自动化测试解决方案. [Te ...

  6. DevExpress v18.1新版亮点——Office File API篇

    用户界面套包DevExpress v18.1日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress Office File API v18.1 的新功能,快来下载试 ...

  7. android lombok 使用

    把get /set / toString/hash/equal等方法从源文件中简化掉直接编译到二进制文件中 地址https://github.com/rzwitserloot/lombok 一 安装l ...

  8. Java基础第4天

    程序结构(按执行流程划分) 顺序结构:整体上程序是顺序结构. 分支结构:if(如果有else,则必有一个会执行)switch-case 循环结构:for while do-while ,重点:嵌套循环 ...

  9. java语言登陆界面(菜鸟版)

    最近在看的Java入门书是<Head First Java>,一本很棒的Java书. 老师要求的程序流程图我没有,之前我们的做法是写完代码再画流程图,我想这样的做法是不对的,流程图应该是在 ...

  10. L257 论述型作文模板

    一引出相同观点: 1.With the development of science and technology, more and more people believe that...随着科技的 ...