Skip to content

Instantly share code, notes, and snippets.

View squaremo's full-sized avatar
💭
how does computer

Michael Bridgen squaremo

💭
how does computer
View GitHub Profile
@squaremo
squaremo / pubsub
Created October 15, 2010 17:06
Using pub and sub message streams
var msg = require('./messages'), net = require('net');
s1 = new msg.MessageStream(net.createConnection(9000));
s1.send('pub');
s2 = new msg.MessageStream(net.createConnection(9000));
s2.send('sub');
s2.on('message', function(m) { console.log(m.toString()); });
s1.send('Hello world!');
@squaremo
squaremo / README.md
Created May 3, 2012 20:49
Socket handoff in Node.JS

Same idea as https://gist.github.com/2500291, with node.js. Do

$ node forkpeer.js

It'll print out its pid. Then

$ curl http://localhost:8000/

To reload it, use

@squaremo
squaremo / Push event.json
Created July 16, 2012 15:18
organisation events
{
"org": {
"login": "rabbitmq",
"gravatar_id": "d67216c2433a2f9e836e4491ff72a3ad",
"avatar_url": "https://secure.gravatar.com/avatar/d67216c2433a2f9e836e4491ff72a3ad?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-orgs.png",
"id": 96669,
"url": "https://api.github.com/orgs/rabbitmq"
},
"type": "PushEvent",
"created_at": "2012-07-16T12:20:20Z",
@squaremo
squaremo / eventS.js
Created September 10, 2012 19:42
Various ways of implementation stream and lazy-seq like things
// Actually you can hook #1 up as an event handler too, using the
// promise implementation:
function eventsS() {
var step = function(sync) {
return function(cons, nil) {
sync.then(function(cell) {
cons(cell.value, cell.tail);
});
}
@squaremo
squaremo / constructor_inheritance.js
Last active July 24, 2021 06:23
Snippets for PMD post
function Bloop() { }
var bloop = new Bloop();
bloop.constructor === Bloop; // and indeed,
bloop.constructor === Bloop.prototype.constructor;
@squaremo
squaremo / repl.js
Last active December 15, 2015 18:39
Examples of multiple dispatch in JavaScript
widgetize.method(Error, Object, function(err) {
return err;
});
render.method(Error, Function, function(err, append) {
append($('<span/>').addClass('error').text(err));
});
// A special value to represent values we're waiting for
function Waiting() {}
widgetize.method(Waiting, Object, function(wait) {
// Convert a promise-oriented API into a callback-oriented API
function identity(x) { return x; }
function slice(arrayish, arg1, arg2) {
return Array.prototype.slice.call(arrayish, arg1, arg2);
}
// Makes a transformer from promise-returning functions to
// callback-accepting functions.
@squaremo
squaremo / stress.js
Created October 2, 2013 23:55
A variety of stress test for amqplib
var PassThrough = require('stream').PassThrough ||
require('readable-stream/passthrough');
var HWM = 100;
var tasks = new PassThrough({objectMode: true, highWaterMark: HWM});
var conn, ch;
var i = 0;
@squaremo
squaremo / base_types.js
Last active December 27, 2015 18:59
Code snippets for AMQP codec post
var Octet = rangeInt('octet', 0, 255);
var ShortStr = label('shortstr',
transform(function(s) {
return s.substr(0, 255);
}, arb.Str));
var LongStr = label('longstr',
transform(
function(bytes) { return new Buffer(bytes); },
@squaremo
squaremo / client.py
Created February 25, 2014 13:27
RabbitMQ primer RPC
import pika
import sys
import logging
logging.basicConfig(level=logging.CRITICAL)
params = pika.URLParameters('amqp://guest:guest@localhost:5672/%2F')
conn = pika.BlockingConnection(params)
ch = conn.channel()