Skip to content

Instantly share code, notes, and snippets.

View ndelangen's full-sized avatar
🇳🇱
yes?

Norbert de Langen ndelangen

🇳🇱
yes?
  • Chroma Software
  • Netherlands
View GitHub Profile
(function ($) {
var elements = {
html: document.documentElement
body: document.body
};
$(elements.body).one('touchstart', function () {
$(elements.body).off('mouseenter.cursor-detection');
});
$(elements.body).one('mouseenter.cursor-detection', function () {
@ndelangen
ndelangen / simple-fork-example.js
Created January 2, 2017 14:20
Fork can spawn a new NodeJS process. You give it a javascript file to execute.
const fork = require('child_process').fork;
const program = path.resolve('other.js');
const child = fork(program);
@ndelangen
ndelangen / silent-fork-process.js
Created January 2, 2017 14:22
A simple way to just create a silent child process is to pass a options object and set the `silent` property
const fork = require('child_process').fork;
const program = path.resolve('other.js');
const child = fork(program, [], {
silent: true
});
@ndelangen
ndelangen / add-ipc-communication-fork.js
Last active January 2, 2017 14:40
To communicate with the child process we need to enable.
const fork = require('child_process').fork;
const program = path.resolve('program.js');
const parameters = [];
const options = {
stdio: [ 'pipe', 'pipe', 'pipe', 'ipc' ]
};
const child = fork(program, parameters, options);
@ndelangen
ndelangen / child.js
Last active December 5, 2022 18:38
NodeJS child_process communication (IPC) example
if (process.send) {
process.send("Hello");
}
process.on('message', message => {
console.log('message from parent:', message);
});
@ndelangen
ndelangen / spawn-node-ipc.js
Created January 2, 2017 15:01
Spawn can be used to create a process from any command.
const spawn = require('child_process').spawn;
const command = 'node';
const parameters = [path.resolve('program.js')];
const child = spawn(command, parameters, {
stdio: [ 'pipe', 'pipe', 'pipe', 'ipc' ]
});
@ndelangen
ndelangen / process1.js
Created January 2, 2017 15:06
If you have 2 independent NodeJS processes running and want them to communicate, this can be done reliably using a npm package: node-ipc
const ipc = require('node-ipc');
ipc.config.id = 'a-unique-process-name1';
ipc.config.retry = 1500;
ipc.config.silent = true;
ipc.serve(() => ipc.server.on('a-unique-message-name', message => {
console.log(message);
}));
ipc.server.start();
@ndelangen
ndelangen / cluster-example.js
Created January 2, 2017 15:55
The cluster module allows you to easily create child processes that all share server ports.
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
@ndelangen
ndelangen / cluster-ipc.js
Created January 2, 2017 16:18
A NodeJS cluster communicating via ipc.
const cluster = require('cluster');
const http = require('http');
if (cluster.isMaster) {
// init cluster
require('os').cpus().forEach(() => {
cluster.fork();
});
// add eventlisteners
@ndelangen
ndelangen / grid.js
Created January 26, 2017 22:31
A styled-components grid component
import React from 'react';
import styled from 'styled-components';
const breakpoints = {
xs: '(max-width: 400px)',
sm: '(min-width: 401px) and (max-width: 520px)',
md: '(min-width: 521px) and (max-width: 720px)',
lg: '(min-width: 721px) and (max-width: 980px)',
xl: '(min-width: 981px)',
};