📜 ⬆️ ⬇️

A little note on how to make friends with Heroku, Kraken.js and Sockjs

Some time ago, in search of new tools for the implementation of the next “home” project, I came across Kraken.js - an Open Source project from PayPal. Kraken.js is a regular Node.js framework based on express. Searching on Habré, I did not find, absolutely, nothing. I met only one mention as a link to the main site here .

What did he attract me with, and how does he differ from the famous Derby.js , Meteor.js , Sails.js , and others?

And I liked it primarily because it does not impose on the developer very severe restrictions (primarily on data sources, package managers, ...), and at the same time introduces some structuredness in the code, offering to follow the MVC model. I don’t want to dwell on all its buns and features here, since all is well written on the project’s website, but I’ll go straight to “my sheep”.
')
So, the task is to upload the Kraken.js application to the Heroku service, make it work there, and, for sweetness, screw the Sockjs.

I will make a reservation that I performed all the actions described below on Ubuntu 13.10, but I think that for other OS there should not be any strong differences. To start, go to heroku.com , register and download the Heroku Toolbelt - a console client for working with Heroku. Enter in the command line:
$ heroku login 

Now we are ready to deploy applications on Heroku. The next step is to install Kraken.js, to do this:
 $ sudo npm install -g generator-kraken 

Then we go to the directory with the projects and type the command $ yo kraken and interactively answer the questions of the generator:
      , "" "`.
     / _ _ \
     | (@) (@) |  Release the Kraken!
     ) __ (
    /, ')) ((`. \
   (((())))
    `\`) ('/'

 [?] Application name: Kraken-Sockets
 [?] Description: A test kraken application with socks.js
 [?] Author: <your name>
 [?] Use RequireJS?  (Y / n) n

The generator created the directory of the same name to the application and the basic structure of the Kraken.js application. Consider the structure in more detail:
 /config        json /controllers      /lib        /locales   /models  /public Web-,  /public/templates    /tests   - index.js     () 

We type the npm start command and at localhost: 8000 we can admire a working Kraken.js application. Well, now it's time to pour it on Heroku. But first you need to bring the application into compliance with the requirements . We already have the package.json file, now we need to create a Procfile file with the following content:
 web: node index.js 

This file is a guide for Heroku how to run our application. In addition to these files, Heroku requires the application to listen to the port specified in the PORT environment variable. We cannot do this through json-configs. Especially for these cases, Kraken.js provides a software configuration mechanism. The method responsible for the software configuration is in the index.js file. We must give it the following form:
 app.configure = function configure(nconf, next) { // Async method run on startup. nconf.set('port', Number(process.env.PORT || 5000)); next(null); }; 

And now let's create a project git repository:
 $ git init $ git add . $ git commit -m "init" 

And execute:
 $ heroku create kraken-test-socksjs 

Heroku will create us an application available at kraken-test-socksjs.herokuapp.com and immediately connect us to a remote repository, push which installs the application on the server.
We try:
 $ git push heroku master 

We go on kraken-test-socksjs.herokuapp.com and make sure that everything works. But we are not in a hurry to rejoice; The very first commit (for example, add something to the template of the main page) breaks the entire application. Then it took me many hours to view the logs ( $ heroku logs ) and google. And everything turned out to be very simple. Heroku, for some reason, loses some dependencies of the second level (dependencies of dependencies of the application - it sounds horrible, I will give an example: in our case it loses the formidable module, on which the kraken-js module depends). And he also doesn’t perform grunt tasks for resource preparation (compiling less, templates, etc.).

To eliminate these problems, do the following: in the package.json file, we transfer all devDependencies to dependencies and add the "grunt-cli": "~0.1.13" module to dependencies "grunt-cli": "~0.1.13" . This will allow us to call grunt manually during the application deployment. After all changes, the package.json file should look like this:
 { "name": "kraken-sockets", "version": "0.1.0", "description": "", "author": "Your Name", "main": "index.js", "scripts": { "test": "grunt test", "start": "node index.js", "postinstall": "./node_modules/grunt-cli/bin/grunt build --force" }, "engines": { "node": ">=0.10.0" }, "dependencies": { "kraken-js": "~0.7.0", "express": "~3.4.4", "adaro": "~0.1.x", "nconf": "~0.6.8", "less": "~1.6.1", "dustjs-linkedin": "~2.0.3", "dustjs-helpers": "~1.1.1", "makara": "~0.3.0", "mocha": "~1.17.0", "supertest": "~0.8.2", "grunt": "~0.4.1", "grunt-cli": "~0.1.13", "grunt-contrib-less": "~0.9.0", "grunt-dustjs": "~1.2.0", "grunt-contrib-clean": "~0.5.0", "grunt-contrib-jshint": "~0.6.4", "grunt-mocha-cli": "~1.5.0", "grunt-copy-to": "0.0.10" }, "devDependencies": { } } 

Pay attention to the "postinstall" script, which is just preparing application resources. We coped with the problem of resources, and what to do with the missing modules? This is apparently a bug of the new module caching system Heroku. I'll write a ticket to them the other day, but for now I cope with the help of the heroku-repo plugin. Just before each “push” release, I clean the modules cache with the command:
 $ heroku repo:purge_cache -a kraken-test-socksjs 

Wow, with the friendship of Heroku and Kraken.js managed, now the most pleasant thing: let's tie Sockjs to the Kraken .

Sockjs is distributed as an npm-module and is perfectly screwed to the express-application, but in order to fasten it you need access to the server (the one represented by the http module, on which express.js is hung). So in Kraken.js it is well hidden from the developer. But, rummaging through the source code of the Kraken, I found how to get it out. I will cite immediately the ready code, which should be inserted into index.js in the place of this site:
 kraken.create(app).listen(function (err) { if (err) { console.error(err.stack); } }); 

Here he is:
 var k = kraken.create(app); k.listen(function (err) { if (err) { console.error(err.stack); } else { var http = k.app.get('kraken:server'); var sockjs_opts = {sockjs_url: 'http://cdn.sockjs.org/sockjs-0.3.min.js'}; var sockjs_echo = sockjs.createServer(sockjs_opts); sockjs_echo.on('connection', function(conn) { hub.sockjs_pool.push(conn); conn.on('data', function(message) { hub.sockjs_pool.forEach(function(con) { con.write(message); }); }); }); sockjs_echo.installHandlers(http, {prefix:'/echo'}); } }); 

At this point I am rounding out. All project code can be viewed on github . Play with the deployed application here .

I really hope that this article will help someone save time when debugging Kraken.js applications on Heroku and screwing all sorts of goodies to them.
PS This is my first article, so constructive criticism is very welcome.

Source: https://habr.com/ru/post/213199/


All Articles