Skip to content

Instantly share code, notes, and snippets.

View robzhu's full-sized avatar

Robert Zhu robzhu

View GitHub Profile
@robzhu
robzhu / serverWithActions.js
Last active January 7, 2019 23:22
WS server refactored to split execution logic from state
const WebSocket = require('ws');
const short = require('short-uuid');
const connections = {};
const send = (connectionId, data) => {
const connection = connections[connectionId];
connection.send(data);
}
const defaultActions = {
@robzhu
robzhu / server.js
Created January 4, 2019 23:18
Local WebSocket Echo server
const WebSocket = require('ws');
const wss = new WebSocket.Server({port: 8080});
wss.on('connection', socket => {
socket.on('message', data => {
socket.send(data);
});
});
console.log(`Listening on ws://localhost:8080`);
@robzhu
robzhu / client.js
Created January 4, 2019 23:04
WebSocket client.js
const WebSocket = require('ws');
const readline = require('readline');
const url = process.argv[2];
const ws = new WebSocket(url);
ws.on('open', () => console.log('connected'));
ws.on('message', data => console.log(`From server: ${data}`));
ws.on('close', () => {
console.log('disconnected');
@robzhu
robzhu / repro.js
Created September 8, 2017 15:55
GraphQL "type" field name repro
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLList,
} = require('graphql');
const helloType = new GraphQLObjectType({
name: 'helloType',
fields: () => ({
@robzhu
robzhu / subscribe.js
Last active July 14, 2017 18:35
Alternate createSourceEventStream implementation using wrapped promise
function createSourceEventStream(
schema: GraphQLSchema,
document: DocumentNode,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{[key: string]: mixed},
operationName?: ?string,
fieldResolver?: ?GraphQLFieldResolver<any, any>
): Promise<AsyncIterable<mixed>> {
return new Promise((resolve, reject) => {
@robzhu
robzhu / DispatchQueue.js
Last active December 27, 2016 21:04
DispatchQueue for managing request rate against an API call
import sleep from 'sleep-promise';
class DispatchQueue {
constructor(rateLimitInSeconds) {
this.rateLimit = rateLimitInSeconds * 1000;
this.queueSize = 0;
}
async dispatch(call) {
this.queueSize++;
@robzhu
robzhu / Vagrantfile
Created April 18, 2016 01:08
Vagrantfile for building a trusty64 box as a docker host
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network "private_network", ip: "192.168.10.101"
config.vm.provider "virtualbox" do |vb|
vb.memory = 2048
vb.cpus = 2
end
config.vm.provision "shell", inline: <<-SHELL
@robzhu
robzhu / new-service.sh
Created September 12, 2015 02:44
init.d service creation script
#!/bin/bash
SERVICE_FILE=$(tempfile)
echo "--- Download template ---"
wget -q -O "$SERVICE_FILE" 'https://gist.githubusercontent.com/robzhu/2772bc0d8cb05e45d639/raw/c6cbfe996431d9e4f9224bdec0208eb19b0e45c1/service.sh'
chmod +x "$SERVICE_FILE"
echo ""
echo "--- Customize ---"
@robzhu
robzhu / service.sh
Created September 12, 2015 02:43
init.d service template
#!/bin/sh
### BEGIN INIT INFO
# Provides: <NAME>
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: <DESCRIPTION>
### END INIT INFO
@robzhu
robzhu / _service.md
Last active September 12, 2015 02:32 — forked from naholyr/_service.md
Sample /etc/init.d script

Sample service script for debianoids

Look at LSB init scripts for more information.

Usage

Copy to /etc/init.d:

# replace "$YOUR_SERVICE_NAME" with your service's name (whenever it's not enough obvious)