Blog Spot!


600k concurrent websocket connections on AWS using Node.js

Added on 09.Aug.2017
Tags: nodejs settings cfg concurrent

Nonaggregated Column error, only in MySQL 5.7.5+

HOW TO

-- list all global variables
SHOW GLOBAL VARIABLES WHERE Variable_name = 'sql_mode'

..get sql_mode value - remove ONLY_FULL_GROUP_BY

-- save `sql_mode` new value
SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

must reconnect to work!

Added on 04.Aug.2017
Tags: sql mysql sql_mode

Extended implementation of async.waterfall

Extended implementation that allows functions to pass a varying number of arguments along.

function waterfall(tasks, cb) {
    cb = cb || function() {};

    // support for arbitrary # of arguments passed along by functions
    var data = Array.prototype.slice.call(arguments, 2);

    if (!Array.isArray(tasks)) {
        return cb('tasks must be an array');  
    }

    // check if there are functions left
    if (!tasks[0]) {

        // avoid race condition with 
        if (data.length > 1) {
            cb(null, data);
        }  else {
            cb(null, data[0]);
        }
        return;
    }

    // process the next function
    var next = tasks[0];
    data.push(function(err) {
        if (err) {
            return cb(err);
        }

        // build the args for waterfall...
        var args = [];
        // remaining functions    
        args.push(tasks.slice(1));
        // final waterfall callback
        args.push(cb);
        // arbitrary # of args for next function
        args.push.apply(args, Array.prototype.slice.call(arguments, 1)); 

        process.nextTick(function() {
            waterfall.apply(null, args);
        });
    });

    next.apply(null, data);
};
// test
waterfall([
    function(next) {
        console.log('1st');
        setTimeout(next, 1000);
    },

    function(next) {
        console.log('2nd');
        setTimeout(next, 1000);
    },

    function(next) {
        console.log('3rd');
        setTimeout(next, 1000);
    },

    function(next) {
        console.log('4th');
        next(null, 'Hello!');
    },
], function(err, res) {
    console.log(err, res);
});

jsfiddle

Added on 19.Jun.2017
Tags: js node async waterfall nodejs

Simple implementation of async.waterfall

function waterfall(arr, cb) {
    var fns = arr.slice(1);

    if (!arr[0]) {
        process.nextTick(function() {
            cb(null);
        });
        return;
    }

    var first = arr[0];

    first(function(err, data) {
         if(err) {
             return cb(err);
         }
         waterfall(fns, cb, data); 
    });
}

waterfall([
    function(next) {
        console.log('1st');
        setTimeout(next, 1000);
    },

    function(next) {
        console.log('2nd');
        setTimeout(next, 1000);
    },

    function(next) {
        console.log('3rd');
        setTimeout(next, 1000);
    },

    function(next) {
        console.log('4th');
        setTimeout(next, 1000);
    },
], function(err) {
    console.log(err);
});

no nodejs environment helper

var process = {nextTick:function(fn){ setTimeout(fn); }};

UPDATE: 2021-10-27

  • Enable value passing to next function here

stackoverflow

Added on 14.Jun.2017
Tags: js node async waterfall nodejs

NodeJS request without a single dependency

/* jshint esversion:6 */

const get = function (url) {

    // return new pending promise
    return new Promise((resolve, reject) => {

        // select http or https module, depending on reqested url
        const lib = url.startsWith('https') ? require('https') : require('http');
        const request = lib.get(url, (response) => {

            // handle http errors
            if (response.statusCode < 200 || response.statusCode > 299) {
                reject(new Error('Failed to load page, status code: ' + response.statusCode));
            }

            // temporary data holder
            const body = [];

            // on every content chunk, push it to the data array
            response.on('data', (chunk) => body.push(chunk));

            // we are done, resolve promise with those joined chunks
            response.on('end', () => resolve(body.join('')));
        });

        // handle connection errors of the request
        request.on('error', (err) => reject(err))
    })
};

get('https://httpbin.org/get')
    .then((body) => console.log(body))
    .catch((err) => console.log(err));

link

Added on 09.Jun.2017
Tags: node nodejs request http get

Search


PHP Libraries


Carbon lib / docs
Idiorm lib / docs
Image Workshop lib / docs
lorenzos/Minixed lib / docs
Parsedown lib / docs
PHP Paginator lib / docs
PHP Redis lib / docs
QrCode lib / docs
Requests lib / docs
Slim lib / docs
Spyc lib / docs
TWIG lib / docs
Upload lib / docs
Validation lib / docs
Zebra Image lib / docs

JS Libraries


AJV lib / docs
BackboneJS lib / docs
Bootstrap Notify lib / docs
C3.js lib / docs
ChartJS lib / docs
FastMD5 lib / docs
HighlightJS lib / docs
jQuery-Storage lib / docs
JS-Cookie lib / docs
Leaflet JS lib / docs
LowDB lib / docs
Marked lib / docs
NeedlyJS lib / docs
ParcelJS lib / docs
RequireJS lib / docs
Swig lib / docs
Toastr lib / docs
Underscore lib / docs
ValidateJS lib / docs
top