Skip to content

Instantly share code, notes, and snippets.

@ktkaushik
ktkaushik / basic
Created November 15, 2012 06:16
Faye pub/sub for rails
It uses WebSocket, EventSource, or HTTP polling to establish a persistent connection between every client on the server. It uses the best network transport available in the user's browser. The server's job is to accept messages from clients and route them to other clients based on the channels that each client is subscribed to. If a client has a socket connection, the message is pushed immediately. If it has an HTTP connection, the message is queued on the server and returned the next time the client sends a polling request -- this makes sure that clients don't drop messages while they are not connected to the server.
@ktkaushik
ktkaushik / broadcast_message.rb
Created November 16, 2012 12:12
broadcast message to faye channel using net/http
def broadcast_message
uri = URI.parse('http://ws.192.168.2.3.xip.io:9292/faye')
message = {:channel => '/devices/location/coordinates/b8de8b239eafb3f7', :data => "this is a test message"}
Net::HTTP.post_form(uri, :message => message.to_json)
end
@ktkaushik
ktkaushik / curl
Created November 19, 2012 10:50
curl for Faye
curl http://your_domain_com_and_port_no/faye -d 'message={"channel":"your/channel/here", "data":["latitude_here","longitude_here"]}'
#ex:
curl http://ws.192.168.2.4.xip.io:9292/faye -d 'message={"channel":"/devices/location/coordinates/b8de8b239eafb3f7", "data":["52.38021","4.895679"]}'
@ktkaushik
ktkaushik / anotherVersion.js
Created November 23, 2012 19:25
check if browser is WebSocket compatible
$(document).ready(function() {
if( typeof(WebSocket) != "function" ) {
// Display error message here !
}
});
@ktkaushik
ktkaushik / index.html
Created November 25, 2012 09:43
A CodePen by Seth Abbott. CSS Swinging Panel Menu - A simple, clean pure CSS menu that uses keyframe animation to create a "swining panel" effect for sub nav. I am having trouble getting it to work in Opera but performs well in Chrome, Safari and Firefox
<div id="container">
<ul id="menu">
<li><a href="#">About Me</a>
<ul>
<li><a href="#">Lorem ipsum dolor</a></li>
<li><a href="#">Maecenas lacinia sem</a></li>
<li><a href="#">Suspendisse fringilla</a></li>
</ul>
</li>
<li><a href="#">Portfolio</a>
@ktkaushik
ktkaushik / index.html
Created November 25, 2012 09:49
A CodePen by Kaushik. CSS Chronograph - Experimenting with ways to visualize time via CSS
<div class="timer-group">
<div class="timer hour">
<div class="hand"><span></span></div>
<div class="hand"><span></span></div>
</div>
<div class="timer minute">
<div class="hand"><span></span></div>
<div class="hand"><span></span></div>
</div>
<div class="timer second">
@ktkaushik
ktkaushik / possibility_2.js
Created December 8, 2012 12:08
JSON format for reports
[
{
device_id: 6,
outgoing: 6,
incoming: 5
},
{
device_id: 5,
outgoing: 6,
incoming: 5
@ktkaushik
ktkaushik / index.haml
Created December 14, 2012 05:38
A CodePen by Kaushik. Slide out search bar - A search icon that reveals a search bar on hover
%html
%body
%div.container
%div.search-container
%input{:type => "text", :placeholder => "search"}
%a.button
%i.icon-search
@ktkaushik
ktkaushik / .irbrc
Created December 19, 2012 03:40
My .irbrc file on mac
require 'irb/completion'
require "irb/ext/save-history"
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"
# Break out of the Bundler jail
# from https://github.com/ConradIrwin/pry-debundle/blob/master/lib/pry-debundle.rb
if defined? Bundler
@ktkaushik
ktkaushik / converter.rb
Last active December 10, 2015 06:48
Converting distance_of_time_in_words back to time
# DOES NOT HANDLE 'half a minute' AND 'about a minute' among others
# dotiw is the distance_of_time_in_words
if dotiw
# split it with ' ' and get an array.
# Your array contains things such as :
# ["1", "year,", "11", "months,", "27", "days,", "13", "hours,", "and", "39", "minutes"]
split_distance_of_time_into_array = dotiw.split(' ')
total_time=0
split_distance_of_time_into_array.each_with_index do |t,index|