Recetas Nginx: notificaciones asíncronas de PostgreSQL a websocket

Para preparar notificaciones asincrónicas de PostgreSQL a websocket, necesitamos nginx y sus complementos postgres , push-stream , set-misc . (Di enlaces a mis bifurcaciones, porque hice algunos cambios que hasta ahora no se pudieron insertar en los repositorios originales. También puede usar la imagen preparada ).

Para conectar clientes a nginx a través de websocket, cree

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 } 

Al conectar un cliente a websocket, comenzamos a escuchar notificaciones asincrónicas en PostgreSQL

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

Al desconectar un cliente de websocket, dejamos de escuchar notificaciones asincrónicas en PostgreSQL

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

Cuando un cliente publica en websocket, hacemos algo

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

Además, puede enviar algo al cliente en 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/456672/


All Articles