This article will provide an overview of the capabilities of the
Nchan module of the
nginx web server, which replaced the deprecated NGiNX_HTTP_Push_Module module. The
Nchan module supports the basic technologies of sending messages Websocket, EventSource (Server-Sent Events), Long-Polling. For horizontal scaling, the redis server cluster is used.
The Websocket technology is still not 100% covered by common web browsers, see
statistics . Yes, do not support only 6% of web browsers. However, if the client has included an item on Opera-mini support in the contract, then fall back on Long-Polling is no longer necessary. There is one more thing that reduces the availability of the Websocket technology, although I can’t find statistics. Maybe someone will tell where to get it. Websocket may not support some DDoS protection systems, providers in some countries with legal restrictions, some mobile Internet providers, as well as filtered when accessing the Internet through a corporate firewall, if so decided to set up system administrators for security reasons or reduce the load.
Another significant issue in Websocket technology is scaling out. I will quote the pm2 documentation (Process Manager for node.js) from the Cluster Mode section.
Statelessify your application
For example, sessions / websocket connections , session-memory and related. Use states, between states.
')
The Twelve Factor Application manifesto.
That is, for horizontal scaling (even if, for example, the node.js application runs on one server, but on several cores), you need to use additional mechanisms to organize a common message bus for the entire cluster.
And finally, a new player has appeared - this is Server-Sent Events technology. Browser support statistics are almost the same as for Websocket (that is, very good),
see statistics . But the use of distribution seems to me still at the initial level (that is, no). The advantage of
Nchan is that the use of all three technologies is transparent for both the client and the server.
The
Nchan module
is included in the nginx-extras build. For the convenience of readers, I have placed
the configuration for docker-compose in my
repository .
For the client there is a library
https://github.com/slact/nchan.js . You can determine which protocol to work with directly or in order of priority. Three protocols are supported: Websocket, EventSource (Server-Sent Events), Long-Polling transparently for the client and for the server.
var opt = { subscriber: 'websocket',
On the server, you must configure the url through which will be listened to and send messages in the topic.
nchan_redis_url "redis://redis"; nchan_storage_engine "redis"; nchan_message_buffer_length 100; nchan_message_timeout 5s; location = /sub { nchan_subscriber; nchan_channel_id $arg_id; nchan_use_redis on; } location = /pub { nchan_publisher; nchan_channel_id $arg_id; nchan_use_redis on; }
For me it was a little surprise that the messages are very well stored by the server, so I set the message storage time for 5 seconds so that when the page is overloaded they are not delivered in one fell swoop, starting from the first. The default value is 3600 seconds.
Finally, you can send a message to the topic using a simple POST request.
curl --request POST --data "test message" http://localhost:8003/pub?id=mytopic
apapacy@gmail.com
May 7, 2018