Skip to content

Instantly share code, notes, and snippets.

View stuartc's full-sized avatar

Stuart Corbishley stuartc

View GitHub Profile
@rlivsey
rlivsey / test_channel.ex
Last active July 22, 2016 10:30
Subscribing to events from a Phoenix.Channel in 1.2.0-rc.0
defmodule MyApp.TestChannel do
use MyApp.Web, :channel
require Logger
intercept ["some-event"]
def join("test:lobby", _, socket) do
# subscribe to a topic
subscribe socket, "some-topic"
@bnorton
bnorton / session.rb
Created August 26, 2015 05:34
Skip the default Rails session (when building an API)
class ApplicationController < ActionController::Base
before_action :skip_session
private
def skip_session
request.session_options[:skip] = true
end
end
@plamb
plamb / streaming_pg_elixir.md
Last active October 1, 2021 05:36
Streaming json from Postgresql through Phoenix

Streaming json from Postgresql through Phoenix

I wanted to explore the fastest way to have json generated in postgresql delivered through phoenix to the browser. With this, I wanted to completely avoid any decoding/encoding happening in ecto/phoenix since I already had perfectly valid json coming out of pg.

It took some trial and error, and some source code reading, and some help from the list but I've got it working with materialized views and returning them in less than 1ms on my laptop.

Here's the short how-to. I'm using pg 9.4 and this should work with both json and jsonb results. I've also used the generic app/App nomenclature, you'll need to change that to your app's name.

Let's take a really, really simple query that returns some json:

@kesor
kesor / an upstart unicorn.conf
Last active February 9, 2022 09:20
Unicorn that receives USR2 signal on upstart's "stop unicorn", but also allows upstart to respawn it when for some reason it crashed on its own.
# unicorn
description "unicorn ruby app server"
start on (local-filesystems and net-device-up IFACE=lo and runlevel [2345])
stop on runlevel [!2345]
env WORKDIR=/data
env PIDFILE=/data/tmp/pids/unicorn.pid
env CFGFILE=/data/config/unicorn.rb
@atenni
atenni / README.md
Last active April 24, 2024 01:36
How to permalink to a gist's raw file

Problem: When linking to the raw version of a gist, the link changes with each revision.

Solution:

To return the first file from a gist: https://gist.github.com/[gist_user]/[gist_id]/raw/

To get a file from multi–file gist: https://gist.github.com/[gist_user]/[gist_id]/raw/[file_name]

@quiver
quiver / README.md
Last active April 3, 2024 15:47
Who says PostgreSQL can't Pub/Sub like Redis?

Pub/Sub pattern with PostgreSQL's LISTEN/NOTIFY command

This is a simple chat-like program using pub-sub pattern, backed by PostgreSQL's LISTEN/NOTIFY command.

Publish

publish message to foo channel from user nickname.

$ python pub.py foo nickname
PUBLISH to channel #foo
@rgreenjr
rgreenjr / postgres_queries_and_commands.sql
Last active June 1, 2024 14:11
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(clock_timestamp(), query_start), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
-- show running queries (9.2)
SELECT pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
@MohamedAlaa
MohamedAlaa / tmux-cheatsheet.markdown
Last active June 2, 2024 11:04
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@leshill
leshill / some_concern.coffee
Created May 14, 2012 17:49
Mixins in CoffeeScript
class SomeConcern
constructor: (klass) ->
_.extend klass.prototype,
doesAllTheThings: ->
true
@jeffreyiacono
jeffreyiacono / Rakefile
Created February 8, 2012 20:15
rake task for precompiling assets using sprockets within a sinatra app + view helpers
require 'rubygems'
require 'bundler'
Bundler.require
require './application'
namespace :assets do
desc 'compile assets'
task :compile => [:compile_js, :compile_css] do
end