Last active
July 18, 2022 09:57
-
-
Save ajaxray/17d6ec5107d2f816cc8a284ce4d7242e to your computer and use it in GitHub Desktop.
Revisions
-
ajaxray revised this gist
Feb 8, 2017 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,12 +3,12 @@ Gathering.js - How to use Keep list (and count) of online users in a Firebase web app - by isolated rooms or globally. Live Demo ------------ [Firebase Shared Checklist](https://fir-app-4125e.firebaseapp.com/) is a demo application that shows the number of users joined a checklist using gathering.js. Here is a [1 minute screencast](https://www.youtube.com/watch?v=buqgHLBe48A) of using this application. You may have a look at the [source code](https://github.com/ajaxray/shared-checklist) to see example usages. Initialization -------------- -
ajaxray revised this gist
Feb 8, 2017 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,6 +3,13 @@ Gathering.js - How to use Keep list (and count) of online users in a Firebase web app - by isolated rooms or globally. Demo ----- [Firebase Shared Checklist](https://fir-app-4125e.firebaseapp.com/) is a demo application that shows the number of users visiting a list using gathering.js. Here is a [video](https://www.youtube.com/watch?v=buqgHLBe48A) of using this application. You may check the source code of this application [here](https://github.com/ajaxray/shared-checklist) Initialization -------------- ```javascript @@ -35,7 +42,7 @@ gathering.join(firebase.auth().currentUser.uid, 'Superman'); // When I am finished with the gathering, I may leave // When browser is closed/refreshed, current user will automatically leave gathering.leave(); // When all user's have left, or the meetup is over, we can remove the gathering -
ajaxray revised this gist
Jul 15, 2016 . 3 changed files with 10 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ var gathering = new Gathering(firebase.database(), 'Gathering Name'); gathering.join(); // Or Join with a unique id // This will ensure distinct presense of a user, even if opened on multiple browser tab or device gathering.join(firebase.auth().currentUser.uid); // Also can set a display name (along with or without unique id) @@ -38,17 +38,17 @@ gathering.join(firebase.auth().currentUser.uid, 'Superman'); // When browser is closed/refreshed, current ser will automatically leave gathering.leave(); // When all user's have left, or the meetup is over, we can remove the gathering gathering.over(); ``` Let's do something with the user count and user name list ----------------- ```javascript var gathering = new Gathering(firebase.database(), 'Gathering Name'); // Attach a callback function to track updates // That function will be called (with the user count and array of users) every time user list updated gathering.onUpdated(function(count, users) { console.log(gathering.roomName + ' have '+ count +' members.'); console.log('Here is the updated users list -'); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,14 +14,6 @@ var Gathering = (function() { return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5); }; function Gathering(databaseReference, roomName) { this.db = databaseReference; @@ -58,7 +50,7 @@ var Gathering = (function() { this.myName = ''; }; this.over = function () { this.room.remove(); }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,17 +5,18 @@ Keep list (and count) of online users in a Firebase web app - by isolated rooms Let's create a new gathering ```javascript firebase.initializeApp({...}); var gathering = new Gathering(firebase.database(), 'A Room Name'); ``` Now you'll have the following functions and properties to work with - * `gathering.join(uid, displayName)` - Add me to gathering. Optionally can provide a `unique id` and `displayName`. * `gathering.leave()` - Leave (that means remove myself from) the gathering * `gathering.over()` - Remove gathering from database * `gathering.onUpdated(callback)` - That function will be called (with user count and hash of users as param) every time the user list changed * `gathering.roomName` - The gathering name * `gathering.myName` - Current users's name in gathering, if joined * `gathering.room` - Firebase Database reference to the gathering room. Can be listened for removal or anything. To see the usages in more details, please check the **firebase-online-user-count-example.md** file -
ajaxray revised this gist
Jul 12, 2016 . 1 changed file with 0 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -62,16 +62,6 @@ var Gathering = (function() { this.room.remove(); }; this.onUpdated = function (callback) { if('function' == typeof callback) { this.room.on("value", function(snap) { -
ajaxray revised this gist
Jul 12, 2016 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,10 @@ * A Javascript module to keep list (and count) of online users in a Firebase web app - by isolated rooms or globally. * * Initial idea from - http://stackoverflow.com/a/15982583/228648 * * @url : https://gist.github.com/ajaxray/17d6ec5107d2f816cc8a284ce4d7242e * @auther : Anis Uddin Ahmad <[email protected]> * * w:ajaxray.com | t:@ajaxray */ var Gathering = (function() { -
ajaxray revised this gist
Jul 12, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ * * Initial idea from - http://stackoverflow.com/a/15982583/228648 * @auther : Anis Uddin Ahmad <[email protected]> on 7/11/16. * w:ajaxray.com | t:@ajaxray */ var Gathering = (function() { @@ -24,7 +24,7 @@ var Gathering = (function() { this.db = databaseReference; this.roomName = roomName || 'globe'; this.room = this.db.ref("gatherings/" + encodeURIComponent(this.roomName)); this.myName = ''; this.user = null; -
ajaxray revised this gist
Jul 12, 2016 . 3 changed files with 25 additions and 24 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -25,8 +25,14 @@ var gathering = new Gathering(firebase.database(), 'Gathering Name'); // Join Anonymously gathering.join(); // Or Join with a unique id // This will ensure unique presense of a user, even if opened on multiple browser tab or device gathering.join(firebase.auth().currentUser.uid); // Also can set a display name (along with or without unique id) gathering.join(null, 'Superman'); gathering.join(firebase.auth().currentUser.uid, 'Superman'); // When I am finished with the gathering, I may leave // When browser is closed/refreshed, current ser will automatically leave @@ -41,16 +47,13 @@ Let's do something with the user count and user name list ```javascript var gathering = new Gathering(firebase.database(), 'Gathering Name'); // Attach another callback function with user list (array) // That function will be called (with the array of users) every time user list changed. gathering.onUpdated(function(count, users) { console.log(gathering.roomName + ' have '+ count +' members.'); console.log('Here is the updated users list -'); for(var i in users) { console.log(users[i] + '(id: '+ i + ')'); } }); ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,14 +28,14 @@ var Gathering = (function() { this.myName = ''; this.user = null; this.join = function(uid, displayName) { if(this.user) { console.error('Already joined.'); return false; } this.myName = displayName || 'Anonymous - '+ randomName(); this.user = uid ? this.room.child(uid) : this.room.push(); // Add user to presence list when online. var self = this; @@ -69,17 +69,16 @@ var Gathering = (function() { } }; this.onUpdated = function (callback) { if('function' == typeof callback) { this.room.on("value", function(snap) { callback(snap.numChildren(), snap.val()); }); } else { console.error('You have to pass a callback function to onUpdated(). That function will be called (with user count and hash of users as param) every time the user list changed.'); } }; } return Gathering; })(); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,16 +5,15 @@ Keep list (and count) of online users in a Firebase web app - by isolated rooms Let's create a new gathering ```javascript var gathering = new Gathering(firebase.database(), 'A Room Name'); ``` Now you'll have the following functions and properties to work with - * `gathering.join(uid, displayName)` - Add me to gathering. Optionally can provide a `unique id` and `displayName`. * `gathering.leave()` - Leave (that means remove from) the gathering * `gathering.end()` - Remove gathering from database * `gathering.onUpdated(callback)` - That function will be called (with user count and hash of users as param) every time the user list changed * `gathering.roomName` - The gathering name * `gathering.myName` - Current users's name in gathering, if joined * `gathering.room` - Firebase Database reference to the gathering room -
ajaxray revised this gist
Jul 11, 2016 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,8 @@ /** * A Javascript module to keep list (and count) of online users in a Firebase web app - by isolated rooms or globally. * * Initial idea from - http://stackoverflow.com/a/15982583/228648 * @auther : Anis Uddin Ahmad <[email protected]> on 7/11/16. * w: ajaxray.com / t: @ajaxray */ var Gathering = (function() { -
ajaxray revised this gist
Jul 11, 2016 . 2 changed files with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,13 +27,13 @@ var Gathering = (function() { this.myName = ''; this.user = null; this.join = function(displayName) { if(this.user) { console.error('Already joined.'); return false; } this.myName = displayName || 'Anonymous - '+ randomName() ; this.user = this.room.push(); // Add user to presence list when online. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,13 +10,13 @@ var gathering = new Gathering(firebase.database(), 'A Name'); Now you'll have the following functions and properties to work with - * `gathering.join(displayName)` - Add me to gathering. Skip the `displayName` to join anonymously * `gathering.leave()` - Leave (that means remove from) the gathering * `gathering.end()` - Remove gathering from database * `gathering.withCount(callback)` - The callback function will be called (with the count as param) every time user count changed. * `gathering.withList(callback)` - The callback function will be called (with the array of names as param) every time the user list changed. * `gathering.roomName` - The gathering name * `gathering.myName` - Current users's name in gathering, if joined * `gathering.room` - Firebase Database reference to the gathering room To see the usages in more details, please check the **firebase-online-user-count-example.md** file -
ajaxray revised this gist
Jul 11, 2016 . 3 changed files with 24 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ Gathering.js - How to use ================= Keep list (and count) of online users in a Firebase web app - by isolated rooms or globally. Initialization -------------- This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,7 @@ * A Javascript module to keep list (and count) of online users in a Firebase web app - by isolated rooms or globally. * * Anis Uddin Ahmad <[email protected]> on 7/11/16. * w: ajaxray.com / t: @ajaxray */ var Gathering = (function() { This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ Gathering.js - Overview ================= Keep list (and count) of online users in a Firebase web app - by isolated rooms or globally. Let's create a new gathering ```javascript var gathering = new Gathering(firebase.database(), 'A Name'); ``` Now you'll have the following functions and properties to work with - * `gathering.join(username)` - Add current user to gathering. Skip the `username` to join anonymously * `gathering.leave()` - Remove current user from the gathering * `gathering.end()` - Remove gathering from database * `gathering.withCount(callback)` - The callback function will be called (with the count as param) every time user count changed. * `gathering.withList(callback)` - The callback function will be called (with the array of names as param) every time the user list changed. * `gathering.roomName` - The gathering name * `gathering.myName` - Current users's name in gathering, if joined * `gathering.room` - Firebase Database reference to the gathering room To see the usages in more details, please check the firebase-online-user-count-example.md file -
ajaxray revised this gist
Jul 11, 2016 . 1 changed file with 6 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,11 @@ Gathering.js - How to use ================= keep list (and count) of online users in a Firebase web app - by isolated rooms or globally. Initialization -------------- ```javascript firebase.initializeApp({...}); // default/global gathering @@ -17,7 +19,7 @@ var chatroom = new Gathering(firebase.database(), 'Special Name'); Joining, Leaving and Ending (removing) a gathering --------------- ```javascript var gathering = new Gathering(firebase.database(), 'Gathering Name'); // Join Anonymously @@ -36,7 +38,7 @@ gathering.end(); Let's do something with the user count and user name list ----------------- ```javascript var gathering = new Gathering(firebase.database(), 'Gathering Name'); // Attach a callback function with user count -
ajaxray revised this gist
Jul 11, 2016 . 2 changed files with 55 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ Gathering.js - keep list (and count) of online users in a Firebase web app - by isolated rooms or globally ================= Initialization -------------- ``` firebase.initializeApp({...}); // default/global gathering var onlineUsers = new Gathering(firebase.database()); // Create an isolated space var chatroom = new Gathering(firebase.database(), 'Special Name'); ``` Joining, Leaving and Ending (removing) a gathering --------------- ``` var gathering = new Gathering(firebase.database(), 'Gathering Name'); // Join Anonymously gathering.join(); // Or Join with a name gathering.join('Superman'); // When I am finished with the gathering, I may leave // When browser is closed/refreshed, current ser will automatically leave gathering.leave(); // When all user's have left, we can remove the room gathering.end(); ``` Let's do something with the user count and user name list ----------------- ``` var gathering = new Gathering(firebase.database(), 'Gathering Name'); // Attach a callback function with user count // That function will be called (with the count as param) every time user count changed. gathering.withCount(function(count) { console.log('There are ' + count + ' members in ' + gathering.roomName); }); // Attach another callback function with user names list (array) // That function will be called (with the array of names) every time user list changed. gathering.withList(function(names) { console.log(gathering.roomName + ' have the following members -'); console.log(names.join(', ')); }); ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,7 +22,7 @@ var Gathering = (function() { this.db = databaseReference; this.roomName = roomName || 'globe'; this.room = this.db.ref("gatherings/" + encodeURIComponent(this.roomName))); this.myName = ''; this.user = null; -
ajaxray created this gist
Jul 11, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,83 @@ /** * A Javascript module to keep list (and count) of online users in a Firebase web app - by isolated rooms or globally. * * Anis Uddin Ahmad <[email protected]> on 7/11/16. */ var Gathering = (function() { var randomName = function () { return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5); }; var getValues = function(object) { var values = []; for(var i in object) { values.push(object[i]); } return values; }; function Gathering(databaseReference, roomName) { this.db = databaseReference; this.roomName = roomName || 'globe'; this.room = this.db.ref("gatherings/" + this.roomName); this.myName = ''; this.user = null; this.join = function(username) { if(this.user) { console.error('Already joined.'); return false; } this.myName = username || 'Anonymous - '+ randomName() ; this.user = this.room.push(); // Add user to presence list when online. var self = this; var presenceRef = this.db.ref(".info/connected"); presenceRef.on("value", function(snap) { if (snap.val()) { self.user.onDisconnect().remove(); self.user.set(self.myName); } }); return this.myName; }; this.leave = function() { this.user.remove(); this.myName = ''; }; this.end = function () { this.room.remove(); }; this.withCount = function (callback) { if('function' == typeof callback) { this.room.on("value", function(snap) { callback(snap.numChildren()); }); } else { console.error('You have to pass a callback function to withCount(). That function will be called (with the count as param) every time user count changed.'); } }; this.withList = function (callback) { if('function' == typeof callback) { this.room.on("value", function(snap) { callback(getValues(snap.val())); }); } else { console.error('You have to pass a callback function to withList(). That function will be called (with the array of names as param) every time the user list changed.'); } }; } return Gathering; })();