Skip to content

Instantly share code, notes, and snippets.

@ajaxray
Last active July 18, 2022 09:57
Show Gist options
  • Select an option

  • Save ajaxray/17d6ec5107d2f816cc8a284ce4d7242e to your computer and use it in GitHub Desktop.

Select an option

Save ajaxray/17d6ec5107d2f816cc8a284ce4d7242e to your computer and use it in GitHub Desktop.

Revisions

  1. ajaxray revised this gist Feb 8, 2017. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions firebase-online-user-count-example.md
    Original 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.

    Demo
    -----
    Live 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)
    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
    --------------
  2. ajaxray revised this gist Feb 8, 2017. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion firebase-online-user-count-example.md
    Original 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 ser will automatically 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
  3. ajaxray revised this gist Jul 15, 2016. 3 changed files with 10 additions and 17 deletions.
    10 changes: 5 additions & 5 deletions firebase-online-user-count-example.md
    Original 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 unique presense of a user, even if opened on multiple browser tab or device
    // 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, we can remove the room
    gathering.end();
    // 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 another callback function with user list (array)
    // That function will be called (with the array of users) every time user list changed.
    // 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 -');
    10 changes: 1 addition & 9 deletions gathering.js
    Original 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);
    };

    var getValues = function(object) {
    var values = [];
    for(var i in object) {
    values.push(object[i]);
    }
    return values;
    };

    function Gathering(databaseReference, roomName) {

    this.db = databaseReference;
    @@ -58,7 +50,7 @@ var Gathering = (function() {
    this.myName = '';
    };

    this.end = function () {
    this.over = function () {
    this.room.remove();
    };

    7 changes: 4 additions & 3 deletions readme.md
    Original 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 from) the gathering
    * `gathering.end()` - Remove gathering from database
    * `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
    * `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
  4. ajaxray revised this gist Jul 12, 2016. 1 changed file with 0 additions and 10 deletions.
    10 changes: 0 additions & 10 deletions gathering.js
    Original file line number Diff line number Diff line change
    @@ -62,16 +62,6 @@ var Gathering = (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.onUpdated = function (callback) {
    if('function' == typeof callback) {
    this.room.on("value", function(snap) {
  5. ajaxray revised this gist Jul 12, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion gathering.js
    Original 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
    * @auther : Anis Uddin Ahmad <[email protected]> on 7/11/16.
    *
    * @url : https://gist.github.com/ajaxray/17d6ec5107d2f816cc8a284ce4d7242e
    * @auther : Anis Uddin Ahmad <[email protected]>
    *
    * w:ajaxray.com | t:@ajaxray
    */
    var Gathering = (function() {
  6. ajaxray revised this gist Jul 12, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gathering.js
    Original 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
    * 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.room = this.db.ref("gatherings/" + encodeURIComponent(this.roomName));
    this.myName = '';
    this.user = null;

  7. ajaxray revised this gist Jul 12, 2016. 3 changed files with 25 additions and 24 deletions.
    29 changes: 16 additions & 13 deletions firebase-online-user-count-example.md
    Original 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 name
    gathering.join('Superman');
    // 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 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(', '));
    // 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 + ')');
    }
    });
    ```
    13 changes: 6 additions & 7 deletions gathering.js
    Original file line number Diff line number Diff line change
    @@ -28,14 +28,14 @@ var Gathering = (function() {
    this.myName = '';
    this.user = null;

    this.join = function(displayName) {
    this.join = function(uid, displayName) {
    if(this.user) {
    console.error('Already joined.');
    return false;
    }

    this.myName = displayName || 'Anonymous - '+ randomName() ;
    this.user = this.room.push();
    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.withList = function (callback) {
    this.onUpdated = function (callback) {
    if('function' == typeof callback) {
    this.room.on("value", function(snap) {
    callback(getValues(snap.val()));
    callback(snap.numChildren(), 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.');
    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;

    })();
    7 changes: 3 additions & 4 deletions readme.md
    Original 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 Name');
    var gathering = new Gathering(firebase.database(), 'A Room 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.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.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.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
  8. ajaxray revised this gist Jul 11, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gathering.js
    Original 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.
    *
    * Anis Uddin Ahmad <[email protected]> on 7/11/16.
    * 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() {
  9. ajaxray revised this gist Jul 11, 2016. 2 changed files with 5 additions and 5 deletions.
    4 changes: 2 additions & 2 deletions gathering.js
    Original file line number Diff line number Diff line change
    @@ -27,13 +27,13 @@ var Gathering = (function() {
    this.myName = '';
    this.user = null;

    this.join = function(username) {
    this.join = function(displayName) {
    if(this.user) {
    console.error('Already joined.');
    return false;
    }

    this.myName = username || 'Anonymous - '+ randomName() ;
    this.myName = displayName || 'Anonymous - '+ randomName() ;
    this.user = this.room.push();

    // Add user to presence list when online.
    6 changes: 3 additions & 3 deletions readme.md
    Original 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(username)` - Add current user to gathering. Skip the `username` to join anonymously
    * `gathering.leave()` - Remove current user from the gathering
    * `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
    To see the usages in more details, please check the **firebase-online-user-count-example.md** file
  10. ajaxray revised this gist Jul 11, 2016. 3 changed files with 24 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion firebase-online-user-count-example.md
    Original 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.
    Keep list (and count) of online users in a Firebase web app - by isolated rooms or globally.

    Initialization
    --------------
    1 change: 1 addition & 0 deletions gathering,js → gathering.js
    Original 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() {

    22 changes: 22 additions & 0 deletions readme.md
    Original 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
  11. ajaxray revised this gist Jul 11, 2016. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions firebase-online-user-count-example.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    Gathering.js - keep list (and count) of online users in a Firebase web app - by isolated rooms or globally
    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
  12. ajaxray revised this gist Jul 11, 2016. 2 changed files with 55 additions and 1 deletion.
    54 changes: 54 additions & 0 deletions firebase-online-user-count-example.md
    Original 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(', '));
    });
    ```
    2 changes: 1 addition & 1 deletion gathering,js
    Original 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/" + this.roomName);
    this.room = this.db.ref("gatherings/" + encodeURIComponent(this.roomName)));
    this.myName = '';
    this.user = null;

  13. ajaxray created this gist Jul 11, 2016.
    83 changes: 83 additions & 0 deletions gathering,js
    Original 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;

    })();