Function.prototype.cbind = function( ...ctx ) {
const func = this;
let ctxs = ctx;
const bound = function( ...ar ) {
return func.call( ...ctxs, ...ar )
}
bound.cbind = function( ...ctx ) {
ctxs = ctx;
return bound;
}
return bound;
}
// псевдокод
const messages = [];
function sendMessages() {
apiCall( messages ); //тут можно и копирнуть массив-очередь в API-метод
messages.length = 0; //тут чистим очередь
}
const lazySendMessages = sendMessages.debounce( 100 /*миллисекунд*/ );
function pushMessage( m ) {
messages.push( m );
if( messages.length < packSize ) lazySendMessages();
else sendMessages();
}
Я делал через VPS, через веб-сокет (ОМГ).