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 ...
随机推荐
- Linux Hadoop集群搭建第二步:--------SSH免密登陆
内容和Linux 搭建 Hadoop集群--Jdk配置相关联 三台虚拟机的操作 Linux SSH免密登陆: 参考网址:http://www.cnblogs.com/999-/p/6884861.ht ...
- Python数据分析中对重复值、缺失值、空格的处理
对重复值的处理 把数据结构中,行相同的数据只保留一行 函数语法: drop_duplicates() from pandas import read_csv df = read_csv(文件位置) n ...
- PLY文件格式
一.PLY简介 PLY文件格式是Stanford大学开发的一套三维mesh模型数据格式,图形学领域内很多著名的模型数据,比如Stanford的三维扫描数据库(其中包括很多文章中会见到的Happy Bu ...
- ssh免密登陆
1:建立新用户hadoop 2:进入/home/hadoop/.ssh/目录 3:所有要免密连接的终端运行: ssh-keygen -t rsa 三次回车后会产生:id_rsa,id_rsa.pub两 ...
- UVALive - 6434 (贪心)
题目链接:https://vjudge.net/problem/UVALive-6434 题意:给你n个数字,要你把这n个数字分成m组,每一组的消耗值定义为改组最大值和最小值之差,要求这m组的消耗值总 ...
- L259
Few things can feel as crushing as being rejected by someone who you're either dating or romanticall ...
- Python 再次改进版通过队列实现一个生产者消费者模型
import time from multiprocessing import Process,Queue #生产者 def producer(q): for i in range(10): time ...
- 大数据-02-Scala入门
Scala 简介 它是一门基于JVM的面向函数和面向对象的编程语言, 它包含了求值表达式,闭包,切片操作,模式匹配,隐式转换等特性. 可变量/不可变量 可变集合/不可变集合.集合操作 函数 值函数 求 ...
- websocket js 代码样例
function StartWebSocket(wsUri) { websocket = new WebSocket(wsUri); websocket.onopen = function(evt) ...
- 构建NDK交叉编译链
有时我们需要单独编译个c文件,生成一个ELF在Android上面跑,NDK提供了一个 make-standalone-toolchain.sh 脚本,用于生成一套特定平台的交叉编译工具链 使用方法如下 ...