pushpin Server-sent events && openresty 集成试用
前边有写过一个简单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
- 测试
通过浏览器打开 http://localhost:7999/sse
发送消息
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 集成试用的更多相关文章
- pushpin openresty 集成试用
pushpin 是一个很不错的将restapi 转换为reailtime api 的proxy,openresty 具有很强的nginx 控制能力 可以方便的用来进行api 的开发,默认其他语言pus ...
- gearman openresty 集成试用
很简单使用了一个openresty 的lua 模块 环境准备 docker-compose 文件 详细配置可以参考 https://github.com/rongfengliang/gearmango ...
- 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 ...
- server sent events
server sent events server push https://html5doctor.com/server-sent-events/ https://developer.mozilla ...
- openresty 集成 keycloak-oauth-oidc
keycloak 是一个比较全,而且比较方便的sso 解决方案,同时为我们提供了灵活的扩展特性 备注: 测试使用docker-compose 运行,对于keycloak 使用pg 数据库做为后端存储 ...
- openresty 集成 sentry 异常系统
sentry 是一个方便的错误异常追踪系统,同时社区也提供了openresty 的lua 包,使用docker-compose 进行测试 备注: sentry 部分的配置来自官方文档 环境准备 doc ...
- openresty 集成lua-resty-mail +smtp2http 扩展灵活的mail 服务
lua-resty-mail 是一个不错的openresty mail 扩展,我们可以用来进行邮件发送,支持附件功能 smtp2http 是一个smtp 服务,可以将smtp 请求数据转换为http ...
- SQL Server Extended Events 进阶 2:使用UI创建基本的事件会话
第一阶中我们描述了如何在Profiler中自定义一个Trace,并且让它运行在服务器端来创建一个Trace文件.然后我们通过Jonathan Kehayias的 sp_SQLskills_Conver ...
- SQL Server Extended Events 进阶 1:从SQL Trace 到Extended Events
http://www.sqlservercentral.com/articles/Stairway+Series/134869/ SQL server 2008 中引入了Extended Events ...
随机推荐
- Remove duplicates from array
//Given a sorted array, remove the duplicates in place such that each element appear only // once an ...
- 在JS文件中,不需要<script>标签
在JS文件中,不需要<script>标签\
- 第三节 java 数组(循环遍历、获取数组的最值(最大值和最小值)、选择排序、冒泡排序、练习控制台输出大写的A)
获取数组的最值(最大值和最小值) 思路: 1.获取最值需要进行比较,每一次比较都会有一个较大的值,因为该 值不确定,需要一个变量进行临储. 2.让数组中的每一个元素都和这个变量中的值进行比较,如果大于 ...
- E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipa ...
- el-container 实践上的布局问题
当自己利用element-ui上面的例子来实现整体布局的时候, 就是自己分开成单独的vue组件时,发现布局是不对的,效果是这样的: 代码是这样的,代码一模一样,只是拆开了各个组件,如下图: 后来发现是 ...
- WebService的一种简单应用方式入门
1.什么是WebService? WebService即Web服务,简单来讲,他就是一种跨编程语言和跨操作平台的远程调用技术. 2.Web服务: Web服务是基于HTTP和XML的技术:HTTP是互联 ...
- python构造IP报文
import socket import sys import time import struct HOST, PORT = "10.60.66.66", 10086 def m ...
- JAVA按顺序播放多个wav音频
用Java按顺序播放多个音频部件,不是同时播放.代码如下: List<String> files = new ArrayList<String>(); files.add(&q ...
- 记第二届CCPC全国女生赛参赛体验
离比赛时间已经有点久了,今天整理博客看到“”参赛体会“”这一分类,觉得记录一下也好 流水账记一下感受 因为题目我已经忘记了.. 第一次..那么久..大概有三个多小时在金牌区吧.. 然后就是一无所出了. ...
- [转]EM算法(Expectation Maximization Algorithm)详解
https://blog.csdn.net/zhihua_oba/article/details/73776553 EM算法(Expectation Maximization Algorithm)详解 ...