In this post I am going to set a node server to use Quickblox.

  1. brew uninstall node
  2. npm install -g express
  3. Starting from express 4.00 you also need to install express generator as mentioned here. npm install -g express-generator
  4. Generate new project: express chat
  5. Install node modules: cd chat/ && npm install
  6. Git-ignore node_modules directory
  7. Start server: DEBUG=chat:* npm start
  8. Check if its working localhost:3000
  9. Install http status codes: npm install http-status-codes --save
  10. Modify routes/index.js to return json instead of rendering index.html:

    router.get('/', function (req, res, next) {
        res.status(HttpStatus.OK).json({title: 'Express'});
    });
    
    
  11. Restart server and test.
  12. Install quickblox: npm install quickblox --save
    1. Modify routes/index.js to initialize quickblox:
    var QB = require('quickblox');
        
    // initalise the environmenet with my application id, authentication key and authentication secret
    QB.init(<CLIENTID>, <KEY>, <SECRET>);
        
    // create an user session
    var params = {login: 'system', password: 'welcome123'};
    QB.createSession(params, function (err, result) {
        if (err) {
            console.log('Something went wrong: ' + err);
        } else {
            console.log('Session created with id ' + result.id);
        }
    });
        
    /* GET home page. */
    router.get('/', function (req, res, next) {
        // list the users currently enrolled
        QB.users.listUsers(function (err, result) {
            for (var i = 0; i < result.items.length; i++) {
                console.log('User ' + result.items[i].user.login + ' is registered');
            }
        });
        res.status(HttpStatus.OK).json({title: 'Express'});
    });
    
  13. Restart and test if users are getting printed.
  14. Shift the QB initialisation code to app.js and make QB a global var as mentioned here, because we have to use QB in multiple routes.

    //Initialize quickblox
    QB = require('quickblox');
        
    // initalise the environmenet with my application id, authentication key and authentication secret
    QB.init(31620, 'HGKzgragj54ktge', 'YeQ-bXJ7asULOU-');
        
    // create an user session
    var params = {login: 'system', password: 'welcome123'};
    QB.createSession(params, function (err, result) {
        if (err) {
            console.log('Something went wrong: ' + err);
        } else {
            console.log('Session created with id ' + result.id);
        }
    });
    
  15. Restart and test if users are getting printed.

References

  1. Node
  2. Quickblox
  3. Express
  4. express command not found in bash after installing it with npm
  5. Global Variable in app.js accessible in routes?
  6. Build a RESTful API Using Node and Express 4
  7. Node.js - RESTful API
  8. npm - QuickBlox JavaScript SDK
  9. Guide for running with node.js, express, jade, and mongodb
  10. npm-http-status-codes
  11. How can run app using Express 4 without ‘DEBUG=node:* ./bin/www’
  12. Quickblox - js sdk