Nginx配方:从PostgreSQL到websocket的异步通知

为了准备从PostgreSQL到websocket的异步通知,我们需要nginx本身及其插件postgrespush-streamset-misc 。 (我提供了到fork的链接,因为我做了一些尚未塞入原始存储库的更改。您还可以使用现成的图像 。)

要将客户端通过websocket连接到nginx,请创建

location =/websocket { push_stream_subscriber websocket; #    websocket push_stream_channels_path $arg_id; #   websocket    id push_stream_websocket_allow_publish on; #   -    push_stream_client_subscribed_request /subscribe; #        push_stream_client_unsubscribed_request /unsubscribe; #        push_stream_client_publish_request /publish; #         websocket } 

将客户端连接到Websocket时,我们开始监听PostgreSQL中的异步通知

 location =/subscribe { internal; postgres_pass ngx; #    PostgreSQL set_quote_json_str $channel $arg_id; #    postgres_query "listen $channel"; #     } 

当客户端与WebSocket断开连接时,我们将停止监听PostgreSQL中的异步通知

 location =/unsubscribe { internal; postgres_pass ngx; #    PostgreSQL set_quote_json_str $channel $arg_id; #    postgres_query "unlisten $channel"; #     } 

当客户发布到websocket时,我们会做一些事情

 location =/publish { internal; postgres_pass ngx; #    PostgreSQL postgres_query "select now()"; #    PostgreSQL      websocket } 

另外,您可以只在websocket中向客户端发送一些内容

 location =/publisher { allow 127.0.0.1/16; #     deny all; #     push_stream_channel_info_on_publish off; #         push_stream_publisher; #   push_stream_channels_path $arg_id; #   websocket    id } 

Source: https://habr.com/ru/post/zh-CN456672/


All Articles