Skip to content

Instantly share code, notes, and snippets.

Anant Narayanan anantn

Block or report user

Report or block anantn

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@anantn
anantn / firebase_workers.js
Created Dec 27, 2012
Firebase: Implementing a worker queue pattern using firebase_queue_pop.js
View firebase_workers.js
var Firebase = require("./firebase-node.js");
function Queue(ref) {
this._ref = ref;
}
Queue.prototype.pop = function(cb) {
this._ref.startAt().limit(1).once("child_added", this._pop.bind(this, cb));
}
@anantn
anantn / firebase_queue_pop.js
Last active Dec 10, 2015
Firebase: Using .push() to maintain a queue and ensuring only one client is able to obtain the head of the queue at a time.
View firebase_queue_pop.js
var Firebase = require("./firebase-node.js");
function Queue(ref) {
this._ref = ref;
}
Queue.prototype.pop = function(cb) {
this._ref.startAt().limit(1).once("child_added", this._pop.bind(this, cb));
}
@anantn
anantn / firebase_remove_pushed.js
Last active Feb 19, 2019
Firebase: Removing an item you've pushed. This snippet shows how to remove an object that you just added using push().
View firebase_remove_pushed.js
function pushSomething(ref) {
// Let's push something. push() returns a reference that you can hold onto!
var justPushed = ref.push({test: "push"});
// We return a reference, but you can also return the name of the newly
// created object with .name().
return justPushed;
}
function removeItem(ref) {
// Now we can get back to that item we just pushed via .child().
@anantn
anantn / firebase_first_item.js
Last active Mar 4, 2016
Firebase: Get the first item in a list. This snippet retrieves only the first item in a list.
View firebase_first_item.js
function makeList(ref) {
var fruits = ["banana", "apple", "grape", "orange"];
for (var i = 0; i < fruits.length; i++) {
ref.push(fruits[i]);
}
}
function getFirstFromList(ref, cb) {
ref.startAt().limit(1).once("child_added", function(snapshot) {
cb(snapshot.val());
@anantn
anantn / firebase_player_assignment.js
Last active Apr 5, 2019
Firebase: Assigning players in a multiplayer game. This snippet assigns you a spot in a game if the game isn't full yet.
View firebase_player_assignment.js
function go() {
var userId = prompt('Username?', 'Guest');
// Consider adding '/<unique id>' if you have multiple games.
var gameRef = new Firebase(GAME_LOCATION);
assignPlayerNumberAndPlayGame(userId, gameRef);
};
// The maximum number of players. If there are already
// NUM_PLAYERS assigned, users won't be able to join the game.
var NUM_PLAYERS = 4;
@anantn
anantn / firebase_create.js
Last active Feb 1, 2019
Firebase: Creating data if it doesn't exist. This snippet creates a user only if it doesn't already exist.
View firebase_create.js
function go() {
var userId = prompt('Username?', 'Guest');
var userData = { name: userId };
tryCreateUser(userId, userData);
}
var USERS_LOCATION = 'https://SampleChat.firebaseIO-demo.com/users';
function userCreated(userId, success) {
if (!success) {
@anantn
anantn / firebase_detect_data.js
Created Dec 18, 2012
Firebase: Detecting if data exists. This snippet detects if a user ID is already taken
View firebase_detect_data.js
function go() {
var userId = prompt('Username?', 'Guest');
checkIfUserExists(userId);
}
var USERS_LOCATION = 'https://SampleChat.firebaseIO-demo.com/users';
function userExistsCallback(userId, exists) {
if (exists) {
alert('user ' + userId + ' exists!');
@anantn
anantn / datachannels.js
Created Oct 31, 2012
Data Channel Example
View datachannels.js
/**
* Assume we've connected a PeerConnection with a friend - usually with audio
* and/or video. For the time being, always at least include a 'fake' audio
* stream - this will be fixed soon.
*
* connectDataConnection is a temporary function that will soon disappear.
* The two sides need to use inverted copies of the two numbers (eg. 5000, 5001
* on one side, 5001, 5000 on the other)
*/
pc.connectDataConnection(5001, 5000);
@anantn
anantn / tamejs_gum.js
Created Oct 18, 2012
TameJS wrapper for getUserMedia
View tamejs_gum.js
function myGetUserMedia(constraints, cb)
{
function errCb(code)
{
cb(code, null);
}
function successCb(stream)
{
@anantn
anantn / getUserMediaDevices.html
Created Sep 18, 2012
navigator.mozGetUserMediaDevices Example
View getUserMediaDevices.html
<html>
<body>
<h1>Device Test</h1>
<ul id="contents">
</ul>
<video id="tehvideo"/>
<audio id="tehaudio"/>
<script>
var devices;
var nav = navigator.QueryInterface(Components.interfaces.nsINavigatorUserMedia);
You can’t perform that action at this time.