Nchan module of nginx web server. Work with Websocket, EventSource (Server-Sent Events), Long-Polling
Websocket technology is still not 100% covered by common web browsers, see statistics. Yes, only 6% of web browsers do not support it. However, if the client has included an Opera-mini support clause in the contract, then fallback on Long-Polling is no longer possible. There is another point that reduces the availability of Websocket technology, though I can’t find statistics. Maybe someone will tell you 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 filtering when accessing the Internet through a corporate firewall, if system administrators choose to configure it for security reasons or to reduce the load.
Another significant problem with Websocket technology is horizontal scaling. I will quote the pm2 documentation (Process Manager for node.js) from the Cluster Mode section.
Statelessify your applicationThat is, for horizontal scaling (even if, for example, the node.js application runs on the same server, but on several cores), additional mechanisms must be used to organize a common message bus for the entire cluster.
Be sure your application is stateless meaning that no local data is stored in the process, for example sessions / websocket connections , session-memory and related. Use Redis, Mongo or other databases to share states between processes.
Another resource on how to write efficient, production ready stateless application is The Twelve Factor Application manifesto.
And finally, a new player has appeared - this is Server-Sent Events technology. The browser support statistics are almost the same as for Websocket (that is, very good), see statistics . But the application of distribution, it seems to me, is still at the initial level (that is, none). The advantage of Nchan is that the application of all three technologies is transparent for both the client and the server.
The Nchan module is included in the nginx-extras assembly. For the convenience of readers, I posted in my repositoryconfiguration for docker-compose.
For the client, there is a library https://github.com/slact/nchan.js . You can determine which protocol to work with explicitly or in order of priority. Three protocols are supported: Websocket, EventSource (Server-Sent Events), Long-Polling transparent for the client and for the server.
var opt = {
subscriber: 'websocket', // 'longpoll', 'eventsource', or 'websocket'
//or an array of the above indicating subscriber type preference
reconnect: undefined, // undefined or 'session' or 'persist'
//if the HTML5 sessionStore or localStore should be used to resume
//connections interrupted by a page load
shared: undefined, // true, or undefined
//share connection to same subscriber url between browser
//windows and tabs using localStorage. In shared mode,
//only 1 running subscriber is allowed per url per window/tab.
}
var sub = new NchanSubscriber('/sub?id=mytopic', opt);
sub.on("message", function(message, message_metadata) {
console.log(message)
console.log(message_metadata)
// message is a string
// message_metadata is a hash that may contain 'id' and 'content-type'
});
sub.start(); // begin (or resume) subscribing
On the server, you need to configure the url through which messages will be listened to and sent to 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;
}
It was a little surprise for me that the messages are stored very well by the server, so I set the message storage time to 5 seconds so that when the page is reloaded, they will not be delivered in one fell swoop starting from the first. The default value is 3600 s.
Well, finally, you can send a message to topic with a simple POST request
curl --request POST --data "test message" http://localhost:8003/pub?id=mytopic
[email protected]
May 7, 2018