Skip to content

Instantly share code, notes, and snippets.

@anantn
anantn / firebase_workers.js
Created December 27, 2012 22:11
Firebase: Implementing a worker queue pattern using 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_queue_pop.js
Last active December 10, 2015 01:19
Firebase: Using .push() to maintain a queue and ensuring only one client is able to obtain the head of the queue at a time.
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 February 19, 2019 23:45
Firebase: Removing an item you've pushed. This snippet shows how to remove an object that you just added using push().
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 March 4, 2016 00:04
Firebase: Get the first item in a list. This snippet retrieves only the first item in a list.
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 January 14, 2023 01:51
Firebase: Assigning players in a multiplayer game. This snippet assigns you a spot in a game if the game isn't full yet.
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 November 20, 2023 23:17
Firebase: Creating data if it doesn't exist. This snippet creates a user only if it doesn't already exist.
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 December 18, 2012 00:54
Firebase: Detecting if data exists. This snippet detects if a user ID is already taken
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 October 31, 2012 22:48
Data Channel Example
/**
* 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 October 18, 2012 01:14
TameJS wrapper for getUserMedia
function myGetUserMedia(constraints, cb)
{
function errCb(code)
{
cb(code, null);
}
function successCb(stream)
{
@anantn
anantn / getUserMediaDevices.html
Created September 18, 2012 17:41
navigator.mozGetUserMediaDevices Example
<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);