Skip to Content

Category Archives: Development

Rails basic API authentication with Sorcery and JWT

APIs have become more and more popular due to increasing demand for development of mobile and single page apps, and so the need of sessionless server-side applications, being those that are token-based the most popular ones now days due to their relative easy implementation. Here you have a great example that uses JWT to secure a microservice-based application.

JWT (or jot) stands for JSON Web Token and as it states it is a web token structured in a JSON format. JWTs are composed of 3 parts: the header, the payload, and the signature. In short, the header contains the type of token and the algorithm for the cryptographic signing; the payload (or claim) contains the information wanted to securely send to the server, and the signature which contains the secret key known only by the server and that is used to decrypt the data contained in the payload. It is not the primary objective of this post to explain in detail what JWT is, so if you want to know more about it you can do it here.

JWTs are used in the next way:

  1. The API server generates the JWT.
  2. The JWT is sent to the client.
  3. The client saves the JWT (i.e. in LocalStorage).
  4. The client sends the JWT back –we’ll send it on a request’s header– to the server.
  5. The server uses the JWT’s payload data for X or Y purpose. In our case to identify the user that is performing the request.

 

Besides JWT, we need an authentication strategy to actually identify users. We’ll use Sorcery for this purpose; I recommend you to go ahead and read the documentation if you haven’t done so. You can implement any other authentication library –or build your own– but for this particular case I personally like Sorcery because it offers all basic authentication features and it is not as overkilling as other libraries.

This is how the general authentication flow looks like:

jwt-diagram

Fig. 1 JWT Flow

Conveniently there is a jwt library that we can use to generate user’s token, so go ahead, read its documentation and install it in your project.

Next, we’ll create a service to provide the JWT, another one to validate it, and a last one to authenticate the user.

app/services/jwt/token_provider.rb

app/services/jwt/token_decriptor.rb

app/services/jwt/user_authenticator.rb

NOTE: In this case we are assuming that JWT is in ‘Authorization’ header with format ‘Bearer xxxx.yyyy.zzzz’, that’s why we are splitting @request_headers['Authorization'].

Implementing our services

We need to generate the JWT after user has successfully logged in:

Or signed up:

The token returned in the response should be properly saved in the client so it is sent back to the server in subsequent requests’ headers.

Then we need to authenticate user on future requests. Adding an authentication method on ApplicationController will let us use it on ApplicationController’s children controllers to protect our private endpoints:

And that’s it, our API is now secured using a sessionless approach. From this point on you can add more complexity using jwt and Sorcery‘s methods to, for instance, make token expire after 1 hour or reset user’s password.

Let me know in the comments section below what you think about this strategy.

0 4 Continue Reading →

Dev jobs: A microservices architecture journey

Recently at Tangosource I was assigned to work on an internal project named Dev Jobs (see dev jobs on iOS and dev jobs on Android), where I had the chance to work next to great teammates and implement a microservice architecture with Ruby on Rails, nodeJS, JWT, Ionic, Docker, and several testing tools, including RSpec, Mocha, Frisby, and Protractor. In this first post, I’ll explain the basics of the microservice architecture.

What is a Microservice Architecture ?

Martin Fowler gives a great definition.

[1]“The microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services, which may be written in different programming languages and use different data storage technologies.”

The Problem

Let’s have some context. We are building a server side application that must support different client side platforms such as:

  • Desktop browsers
  • Mobile browsers
  • Native mobile applications

 

Also, we need to consume an external API service. In our case, Linkedin through Oauth2 for authentication. And the ability for users to chat between each other.

On an architectural matter we still had to decide between monolithic or microservice. So let’s dig a little more on this two.

Monolithic vs MicroServices

Monolithic

If we take this approach, we need to keep the following in mind:

  • Build one application to rule them all.
  • Use one language ( Ruby, PHP, JavaScript, etc. ) we could not take any extra advantages from other programming languages.
  • The architecture needs to be flexible enough to support API through AJAX and render HTML.
  • The development team needs to feel comfortable with the language we picked.
  • Security. We will have a public API to serve mobiles/clients.
  • Best tools for RTCP. So we can have a nice looking chat
  • Testing. build testable code

 

I have seen great monolithic architectures that can support all of this. One of my favorites is RoR engines based or nodeJS services based applications.

MicroServices

In the other hand, if we go for MicroServices we must take in mind the following:

  • Delegate responsibilities in small applications.
  • We can use different languages per service. Which is the best choice per responsibility?
  • How are we going to communicate each service ? Normally with tcp/ssl.
  • How are we going to persist Data through services?
  • Security: Protect the services.
  • Testing: Test all microservices.

 

On a personal note, as a developer working on a microservice platform, I can focus only on the technical details wrapping a specific service. This does not mean I should not understand the whole set of services as a unit.

“Any organization that designs a system (defined broadly) will produce a design whose structure is a copy of the organization’s communication structure.”

— Melvyn Conway, 1967

Having this in mind, we had to start by making technical decisions, such as how to protect our API, how to manage RTCP, by nature HTTP is stateless which means is sessionless so for our case we had to maintain a session through tokens ( JWT ), at first we though maybe nodeJS for RTCP since nodeJS is strong for DIRT ( Data Intensive Real Time )  apps. But we had not only had to take care of the tech devs but also for dev resources, currently at TangoSource we have strong Ruby on Rails devs so we had to work all together and take advantage of the expertise of each team member, our best bet was to use RoR for business logic, and take advantage of nodeJS. So the combination of RoR and nodeJS makes the solution solid and strong.

Solution

For this particular project, we decided to go for the MicroService and have some fun! The first thing that we needed to figure out was defining the responsibilities per service. So we end up with 4 microservices.

  • Ionic
    • More than a service, it’s the client side of the application. Ionic by itself is a front end SDK for hybrid mobile apps using AngularJS.
  • IDP
    • Responsible for (a) providing identifiers for users looking to interact with a system, (b) asserting to such a system that such an identifier presented by a user is known to the provider (c) possibly providing other information about the user that is known to the provider.
    • This may be achieved via an authentication module, which verifies a security token, and that can be accepted as an alternative to repeatedly and explicitly authenticating a user within a security area.
  • SP
    • In charge of persisting all user’s data, including interaction with the system. This service will contain all the business logic
  • Chat
    • In charge of establishing real-time communication among users within the system.

 

We decided to protect the services with JWT

Architecture

devjobsarchitecture

Each square represents a service, and the lines are how they communicate between each other.

Ionic : This might be the most critical service of them all, but not the most important. We have 3 main components:

  • Ionic server
    • An HTTP server in charge of sending all the assets ( HTML, CSS, JS ) to the next component.
  • Ionic Cordova
    • A Mobile SDK, which provides the user’s interaction with the app, and consumes the rest of the microservices as resources. This component runs on top of 3 different type of devices.
      • Android
      • Ios
      • Browser
  • Ionic App
    • Ionic admin panel, this panel allow us to configure GCM and APNS exposing a protected API.

 

IDP : This service is in charge of giving identity to a component/user. This service will expire a valid token with a component/user’s basic information on the JWT’s payload. Similar to memoization, this service will keep track of sessions using Redis on a basic TLS connection. Thanks to the RFC 7519 standard, this service can scale in other microservice based architectures and be reused.

SP : This is an API/Https service protected with JWT. This micro service is in charge of persisting all user’s interaction/information using MySQL with a standard TLS connection. It also manages the business logic.  

Chat : This microservice will provide real-time communication between users. This RTCP (real time control protocol) is being protected with JWT on the handshake process, and persists the communication activity on MongoDB, a NoSQL database.

Conclusion

Taking these kind of decisions is not simple. It requires a lot of experience in order to compare libraries/languages, you must at least know how they work, and it’s even better if you have used them. Remember that you will not build this by yourself, you are part of a team. It’s smart to sit with your team and discuss each individual’s strengths and weaknesses in order to make the best tech decisions.

DO NOT mix responsibilities of the micro services, instead identify your services. Add all the needed documentation to help the team such as /*Comments */,  READMEs, and Wikis.
A microservice architecture is not always the best approach, and could be painful if you are not careful.

3 8 Continue Reading →

Developer levels: the right skill set for the job.

Back when software was developed in Fortran and C, you needed a lot of knowledge to write anything of significance.  Originally, Fortran lacked even the concept of a data structure [1].  In the 1990s, an array was a contiguous chunk of memory containing a fixed number of structs or of pointers. Alternatively, one could create a struct where one field was a pointer to the next struct in the array, a true linked list.  You had to write all the code that manipulated anything.  You had to decide if an array or a linked list was best for each situation.  You had to decide if you were gonna use Quick Sort or needed a Heap Sort or if a Bubble Sort was more than enough.

Additionally, there have always been design patterns and the insight to know when to use one over another; the need for an understanding of programming languages and their compilers which affect everything we do; as well as the operating systems on which everything is built.  This list is long, going down deep all the way to discrete applied mathematics and probability.  There is a lot one can know which affects the quality of the software we write.

Today, everything has changed.  Languages do more and more.  Frameworks are getting better and better.  With Ruby on Rails, inexpert coders have so much functionality built into Ruby and even more built into Rails.  Given the objects which contain data structure and an astonishing amount of algorithms and code doing so much of what you need. The ORM with migrations, algorithms and code doing almost all the database management you need, and generators which create boilerplate code inside an opinionated MVC layout, one can write a pretty complete web application while writing surprisingly little of the actual code.  It can be quite some time before one really must make a deep technical decision which one used to make from day one.

This has changed the nature of writing software.  Back then, no company would hire someone after nine months of training let alone nine weeks … but that happens now and reasonably so.  It is perhaps analogous to the difference between a mechanic who can work with a car and a mechanical engineer who can design a car.  An upshot of this is the question of how do we categorize people writing software. When asked, I figure software people, particularly those who write applications, generally fall into about 3 categories.  This post is in no way scientific. It is written in a definitive voice but understand these are generalizations. This is just for the sake of starting a discussion and being analytical.  I have no proof.  Your mileage may vary.

Coders

Coders know how to … code.  They may no longer be muggles but they are more Ron Weasley than Hermione Granger.  They might know a few incantations but there’s still a lot of smoke.  They often use examples or starter apps.  When hitting a roadblock, which normally comes with an error message, they will put that error message into a search engine and see what comes up on StackOverflow.  They find a solution that sounds good and try it.  If that one fails, they try another. Eventually one works.  They can’t really own their code because so much of it is still magic to them. But, they can write serviceable code that works the way it should work.  Their knowledge is usually narrow, shallow and limited [2].  It should be said most are voracious and learning quickly about all the stuff they don’t know.  They do well in a collaborative team environment especially when they get to pair.  Many will go on to be solid developers.

 1

Developers

Developers have a much broader understanding than Coders of the frameworks they use and languages in which they are written.  They understand their code.  When it breaks, they generally know why and can often fix it.  They understand objects; how to write a good one and, preferably, how to test it well.  They understand where the magic lies and often how it works.  They have an understanding of the underlying technologies (SQL, HTTP, SSH, Unix, etc.) and how to work with them directly.  As they become more senior they will know more about more technologies and understand how and when to use them.  Their knowledge is still clearly limited.  They might know a lot about one area, technology or layer and know little or nothing about others. They do not have the knowledge and experience to architect complex systems well but can build well thought-out features that cut across much of an application and know how to refactor properly to bring simplicity and elegance to a complicated design.

 2

Engineers

Engineers have deep knowledge of most every layer of the system on which they work from the OS and servers on which things run, the compiler that munches their code, through to the languages they use and the frameworks on which their products are built.  They understand patterns and architectures, can determine which is best at this moment and can explain why.  They know where the magic lies and understand how it is implemented and how to extend or fix that magic.  Whereas they often have a degree in computer science, there is a growing number who do not, who have delved in and taught themselves.  They understand their code as well as the underlying technologies and they also understand the theories behind why things should or should not work a particular way.  This gives them the facility to fully understand and design architectures as well as solid objects and well-thought-out tests.

 4

 

So?

When building a team to build your applications, you must know what mix of skills you need and who can provide those in the same way Ford or Tesla will have a mix of skills on a team creating a new car, some deep engineering skills, some mechanic skills.  There are many senior engineers who can architect and understand objects but have fewer skills at coding than some coders.  Many ambitious coders know the mechanics of a specific technology and can, especially with oversight, use that technology more effectively than others with more overall experience and knowledge.  Personally, I have found pairing two such people (one with more specific tech knowledge and one with more in-depth knowledge) can be very powerful.  They can learn from each other and your team gets stronger faster.

We must understand who is who, what to expect from each, how to help each grow to their potential and how to get the best code our team can build.  These are complicated enough that each should be another post in what I expect will become a series.

It is a brave new world in which we build applications. There are amazing advances in technology that allow us to build a better product faster than we could ever build in the past.  It has created the opportunity for many more to be able to contribute in meaningful ways.

Thanks for reading!

This is the first of a series of guest posts by Ken Decanio, a software engineer, architect and product manager.  If you would like to read more of his work, you can find find it at his blog.

 

 

[1] Ask me about the DD Array someday.

[2] Actually, it is preferable that a Coder’s knowledge be narrow and focused but that’s another post.

 

 

Photo credits:

  1. http://www.libstock.com
  2. https://www.thefirehoseproject.com
  3. http://www.intelliware.com
2 6 Continue Reading →

Benefits of WordPress + Docker in a production environment + troubleshooting guide.

Hello,

Even though we are a Rails development team, we use WordPress for our blog. We love Rails, but WordPress provides a robust solution for our blogging needs. And lately, we have been experimenting with Docker + WordPress.

So, why Docker?

There are quite a few straightforward reasons why we use Docker with WordPress, namely:

  • Docker reduces clutter.
  • We can run a couple of Rails applications and WordPress instances on the same server.
  • It helps with version conflict management in Rails apps
  • It allows for the duplication of a production server into staging for testing purposes.

 

We can “dockerize” an application, run it in our laptops, run tests and then deploy with confidence that it will work. If you are new to Docker, you should read What is Docker and Dockerizing applications. So, the plan is to apply this process to our application. However, I found some issues while building the instances. I would like to share those issues and how I managed to solve them.

Creating a WordPress instance

There are multiple instances ready to use on the Docker hub.

Try a simple search. I first tried this instance but didn’t like the lack of documentation, and that I wasn’t able to save the uploaded files in a persistent volume. Then I tried this other one but wasn’t able to modify wp-config.php without some hacking. So, I built my own WordPress image instead.

Dockerfile

This file is responsible for creating the docker instance. You can check the Dockerfile instance here. Let me explain each line, so that it all makes sense.

I created a base image php-fpm that allows you to run any PHP application. It is based on ubuntu:14.04.

The first line is to avoid mysql to ask for a root user and password. Then I updated the apt-cache and install curl and mysql client. Last, I removed the apt-cache to reduce the instance size.

These are environment variables that you can change when you run the instance. I think is important to provide with sensible defaults that allow you to quickly test the instance without much thought.

The base image php-fpm has an index.php file that allows you to test it out. I removed it to provide WordPress a clean folder.

This actually installs WordPress. It’s designed to be in one line so that Docker can cache it completely

Then I copy WordPress to the correct folder.

I copy a template to generate wp-config.php. When the instance starts I run the 40_parse_wordpress_config.sh init script. I use bash to render the template using ENV variables. For instance, ${DB_NAME} will be converted to *wordpress_development*. I found the template parser script here.

The issue is that PHP doesn’t inherit the environment variables, so I had to modify wp-config.php in order to set all the variables that the application needs.

I prepare the files to be accessible by nginx and PHP

This line allows you to create persistent storage for the uploaded files and other application changes.

These lines prepare the init scripts.  I add them and set as executable scripts.

When the instance starts it will run each init script. The last one is 50_init.sh from php-fpm which runs nginx and php-fpm. All the previous scripts should execute and stop to allow the init process to work.

Docker compose

Previously known as fig, Docker’s Compose allows you to run multiple Docker instances together. Since WordPress requires a database instance, I created a docker-compose.yml file.

The “build” attribute is the Dockerfile folder. “Links” allows you to connect it with mysql. “Ports” refers to the exposed ports. The format is <host port>:<instance port>. “Environment” allows you to setup the instance

This is to run the database. The password in MYSQL_ROOT_PASSWORD should be the same as DB_PASS

Creating a data volume

In order to run it in production, you need to save the uploaded files. You can use a data volume for that.

In docker-compose.yml you need to add the following:

And add another image to docker-compose.yml:

So every file that is uploaded or modified will be saved in the wordpressdata image.

Just be careful when you are cleaning up docker and removing old images because you could delete wordpressdata.

Another nginx instance.

If you run the example docker-compose.yml it will work. However, WordPress will be connected to port 80 and we won’t be able to run another instance. We need to add another nginx to act as a reverse proxy.

I added the following lines to docker-compose.yml:

And removed the ports attribute in WordPress. Now the proxy connects to WordPress using the env variables and can expose multiple applications.

Here’s where we end for now. In conclusion, after having solved the minor issues mentioned above, we found Docker to be a very useful and efficient tool to manage distributed applications. Give it it a try when you can. Let me know if you have any questions about how our instance works, or check out our documentation here.

Thanks for reading!

F.

0 7 Continue Reading →

Long live your app: MongoDB Replication

The scenario

Not so long ago a customer requested that we provide the highest server reliability that we could. The application was pretty big, with about seven servers per environment. We needed to increase reliability in web servers and databases. In this article, I’ll explain how we achieved that from the database side.

While searching for options, we found that MongoDB (which was the database engine we were using) offered several options that increase your database reliability, i.e.:
MongoDB – Sharding
MongoDB – Replication

We went for MongoDB Replication –also known as Replica Set– because our customer needed a solution ASAP, and the configuration needed is minimal. Besides, it offers whole database replication, which we needed to implement other security measures, like backups.

Enough context for now, so let’s jump into the action!

Configuring MongoDB

First, it is important to notice one thing, as the name states, MondoDB’s Replication will be copying your database ‘n’ times (once for each replica member). With that said, to obtain the maximum benefit from this practice you will want to have your replica set distributed in N different physical servers (i.e. Ubuntu servers). We’ll explain how this works below.

Having N servers set and ready only to practice could be expensive and time-consuming. Therefore, we’ll be performing all of the examples on this article in one single server –your computer– to allocate all of the replica’s members. And we will be creating different folders and pretend that each one of them is a path to a different physical server. So let’s create folders that will be hosting each replica’s member. In our case, our DB’s path is under ‘/usr/local/var/mongodb/’, so we’ll create three folders (a replica needs at least three members) under that route:

If you don’t have your database’s path, you can get it by running this in MongoDB’s terminal:

Within the output command above, locate “dbpath” inside “parsed”.

Example:

With our fictitious servers ready, the next step is to create three “mongod” clients (one for each replica set member).

Note: you need to leave each client running, so you might want to run them in background by appending an ‘&’ at the end of each command. If you need to bring those commands to the foreground, then execute ‘fg’ in the corresponding window where your background jobs are running. Here is an example of how to run mongod clients:

Explaining commands:

mongod: executes mongodb.
–port: indicates the port that the mongod client will be listening to.
–dbpath: specifies the path to your application’s database. In our case, this is pointing to the directories previously created.
–replSet: sets the name of the replica set.

Now you can connect to each mongod client to start configuring the replica set!

Configuring your Replica Set

To configure your replica set, first you need to be connected to one client. You do it by executing the ‘mongo’ command in your terminal and append the –port option to indicate which client you want to be connecting to. In our case, we will be connecting to our client with port 27018:

Once connected, we can start configuring our replica set.

Defining Replica Set Configuration

Configuring the replica set is pretty simple and straightforward, all we need is to set some basic parameters. Go to the mongo shell you just opened and type:

Explaining options:

rsconf: the name of the object or variable that will contain the replica set configuration.

_id: sets the name of the replica set. This needs to match the –replSet option that you set when executing the mongod client. In our case: rs0.
members: sets all the elements that will be conforming our replica set. Elements should be contained within and array following this format:
member’s _id: the element’s identifier
host: the ip address of the server that is hosting the element. It has to be followed by the port where the mongod client is listening to.

Now, apply the configuration to the replica set. In the Mongo shell, type the following:

If everything went OK, you should see this:

You can see additional information by executing rs.isMaster().

At this point, your replica set is up and running!

So you might be thinking now, “so far so good, but I haven’t seen any real advantages yet…”

Let us explain a bit more. What makes MongoDB Replication a good strategy to increase your app’s reliability is the fact that your database will be always available for your web application. You might be wondering ‘How is that possible?’ As you might be guessing, having your database replicated N times gives you the opportunity to prevent data loss and sustain a persistent connection between your database and your web app.

MongoDB Replication consists on having a database replicated N times, where one element is the Master/Primary (this is the only one interacting directly with your app) and the rest of them are Slaves/Secondaries (these are interacting with the master).

This is how it works: when the master element dies, MongoDB automatically chooses a slave element and turns it into a master that will be accepting all IO operations. Your database will be always ‘alive’.

Browsing Secondary elements

You can see your replica set slave elements by logging into a mongo shell for each one of them and typing rs.isMaster(). Don’t forget to set the –port option like this:

Once you are in the mongo shell, the first indicator that you will see is:

Then, after you executed rs.isMaster(), you should see something like:

Where “ismaster” and “secondary” will tell you that that specific client is an slave element. Also, you can see that “primary” option is indicating the IP address of the element that is the master.

And that’s it! You are done configuring MongoDB! Continue next to see how to configure your Rails app to make it work with the replica set.

Configuring the Rails application

If you do not have a Rails application created go ahead and create a new one. Since we are not going to use ActiveRecord, you can append –skip-active-record to the “rails new” command to skip ActiveRecord’s installation and configuration.

Once you have the new Rails app, you will need to include Mongoid in your Gemfile and execute “bundle install”.
Execute “rails g mongoid:config”. This will create config/mongoid.yml where you will have to add the configuration for your app’s db. The configuration of this file is a little bit different than usual, so here you have an example of how it would look for our exercise:

As you can notice, the three hosts are the MongoDB clients we created at the beginning; make sure they are still up and running.

After configuring mongoid.yml, go ahead and create a controller, a model and some views. You can use “rails g scaffold” to speed up the process ;). In this case, we are going to create the basic CRUD for users.

Now go ahead and start the Rails server and add some information in your app.

Checking that MongoDB Replication is working.

Now we are ready to test our replica set, we need to log into the three mongod clients:

Inside the client with port 27018 (the master) check that the database you set inside mongoid.yml was created:

You should see the database you used in your mongoid.yml, in our case we have this:

Then select your database, in our case:

And check that the information you captured in your web app is there:

You should see the data you entered through your web application, in our case:

Now go to the slave elements and repeat the process; you should see the same info that is persisting in your master element. Note: when trying to execute “db.users.find({})” you will receive a restriction error message, that’s normal, go ahead and execute:

NOTE: By default MongoDB protects all the info from being read, so with rs.slaveOk() you are telling MongoDB it is OK for an external user to have access to stored data in the replica set.

Testing database reliability: kill the master

Let’s put al of this to a test. Go to your mongod clients and identify the master. If you don’t remember which one is your master, login into a mongo shell of any of the mongod clients and execute “rs.isMaster();”. In the output, look for the field “primary” to get the IP address of the member that is currently the master/primary.

Our master element is the client running in the port 27018, so we will kill it by executing the following:

Now, go back to a Mongo shell and execute rs.isMaster(). In the output, you should see under “primary” that a new member replaced the one that is dead.

Is my application still working?

Go to your browser and enter new information. Everything should be smooth and quiet as usual, your web app is working correctly.

Reviving the dead member

Once you know your web app is still up and running, then go back and execute the mongod client you just killed in the previous step, in our case:

and log into a Mongo shell for that client:

As you may notice, this member is not longer the master; another member has taken its place and has let it with a secondary role. If you want to know which is the new primary you can use rs.isMaster() again and identify who is the new master.

But what happened with the info save while this member was gone?
Let’s look for it. Connect to the member you just revived and execute next:

You will see all the information that was added through your web app when this replica member was down:

How cool is this?!

Reconfiguring MongoDB Replication

You might have noticed that even if you kill all mongod clients, the next time you execute them the replica set will still be there. Why is this? Do you remember the output when we executed the command rs.initiate(rsconf)? It read something like:

Yes, the configuration was saved! Therefore, every time you execute mongod clients using the –replSet and the same replica set you used before, then that configuration will be applied for that replica set.

Changing, removing, or adding a replica set member is not too hard. Connect to a mongo shell for the primary mongod client, and set a new newConfig object containing all new members, and then apply those changes with:

That’s it! Now you know all the basics of increasing the reliability of your web application by setting up MongoDB Replication!

Conclusion

A downside to MongoDB replication is that it does not matter if your database is replicated N times on N servers, it is still being centralized. Which means that you could bump into networking performance issues if you do not provide your server with enough resources (RAM, processor, hard drive space) to handle your data.

But! Even in spite of the issue mentioned above, we still believe that MongoDB Replication is a good option when you need a server that is reliable, easy to configure, and fast to set up. If you want to learn more about this technique and squeeze all the power of MongoDB Replication, we recommend you read the official documentation to see all the configuration options you can use. For instance, you could set a hidden replica member to create a backup file of your database and upload it to an AWS S3 bucket!

Thanks for reading! Let us know what you think in the comments.

M.

0 5 Continue Reading →

Angular.JS controller communication

AngularJS is a Javascript framework made by Google. Over the last few years, its popularity has increased exponentially. Its main features are double data binding, DOM control, form validation and the use of reusable components. Plus, it’s fairly easy to use, which makes AngularJS one of the top choices when looking for a Javascript framework.

When we start working with Angular, we come to this question very quickly: how can we set our controllers to communicate with each other? Which option is the best?

It is typically assumed that in Angular.js there should be only one controller per view. Sometimes, however, we have no choice but to have two controllers within the same page, and they will need to interact with each other.

Given that, I’ll go into some of the typical scenarios and how to handle the communication among controllers for each of them. I hope this is of help for some of you.

Father and Son

This is when we have a controller inside another controller. So let’s say that our markup looks like this:

The problem would be, how can we access the model inside the child controller? If we want to get info already set in the other controller, then we could do something like:

But this only works for reading the value that has been already set. If that value changes, our variable in the controller will not be updated. We could fix that with the following code:

But watch out, an even better solution arrives! AngularJS provides us with some amazing functions that we can take advantage of:

This function broadcasts an event to all children controllers. All the controllers that are listening for the ‘name’ event will get notified. The $broadcast function could be useful if you wanted to send information from the parent controller to a child controller.

On the other hand, if what you wanted was to communicate from a child to its parent, we use this method:

Finally, ‘on’ allows us to define which methods we are listening to. It listens for the ‘name’ event‘ emitted’ or ‘broadcasted’ methods depending on the case.

Communication from parent controller

Here’s an example:

In this example, we are sending a message every time that our variable ‘something’ changes in the parent controller. Use of $watch is not necessary. What we need to keep in mind is that our child’s controllers and listener for the event needs to be defined before broadcasting

Communication from child controller

Here’s some markup example for this situation:

And the code:

As you can see, the parent controller is listening for a ‘new message’ event. While in our child controller we are emitting this event every time we call ‘tellToMyParent’. We are calling that function when we click on the ‘Say it’ button, and it is sending the value of the $scope.message, which, in this case, is the value in the textbox.

Brother controllers

The idea sounds pretty good, right? Unfortunately sometimes this isn’t as easy as it sounds. There will be some situations on which we have brother controllers, and we can’t get them to communicate without the use of a parent controller.

Here’s some sample markup:

We know that there is one $rootScope for the Angular application and that we could take advantage of that. The code inside the controllers is interpreted in the child scope. And, if the property does not exist in the child scope, then Angular will look for the parent scope, ending at $rootScope. You can take advantage of this to help them communicate.

For example:

Great, but there is only one thing left: if we use $rootScope.$on in the controller, this will create duplicate bindings. That is because controllers can get instantiated multiple times (every time the user enters the view, basically). So we need to be careful with that and find a way to avoid adding duplicate listeners, which could affect the application’s performance very drastically.

So, we still have one option left. That is, combining broadcast and services; services only get instantiated once, unlike controllers.

Examples:

First option; simple set and get:

Second option, combination with $broadcast:

So, we have reviewed a few different ways of handling communication between controllers depending on the situation. As you can see, Angular.js is very flexible with this, but that does NOT mean that we should not to try to look for better implementations and follow best practices. What do you think about these options to communicate controllers? Do they seem useful? Have you found better ones? Feel free to share in the comments.

Thanks for reading!

J.

0 10 Continue Reading →

Introducing Planning Poker: manage sprint planning and retros like a pro.

planning-id-01

Intro

Are you happy with the app you use to do feature estimation in your Scrum process? We weren’t either. How about the way you collect feedback after a sprint is done? We’ve got you covered.

Introducing Planning Poker: the tool to manage Scrum pointing sessions AND retrospectives in the same place, under an Open Source license, for free! How sweet is that? In this post, we’ll tell you why we built this app, and what were we looking for (spoiler alert: you will see those expectations fulfilled in our app). Then, we’ll show you how it actually works, on a step-by-step tutorial to master the main features. Finally, we’ll refer to the technical stack and the reasons behind choosing those specific technologies. We want to open up a line of communication to receive comments and suggestions for improvement. In the end, this is Open Source, right?

More specifically, in this post we’ll cover the following topics:

  1. Why we built this app
  2. How it works
    1. Pointing sessions
      • Creating a pointing session
      • Setting point values
      • Joining a pointing session
      • Pointing stories
      • Statistics
      • Leaving a pointing session
    2. Retrospective meetings
      • Creating a session
      • Joining a session
      • Adding entries
      • Editing entries
      • Deleting entries
      • Revealing entries
      • Reviewing entries
      • Leaving a session
  3. The technical stack we used to build it.

 

Let’s get started.

Why did we build this app?

Planning Poker was born out of the need to have a fast, simple, and fun tool for the process of estimating the complexity of a feature. At the same time, we needed something to help us manage retrospective meetings. In Planning Poker, the estimation sessions and the retrospectives are carried out in accordance with the Scrum methodology in a distributed environment. We needed an application that kept these processes simple yet flexible, allowing for the addition of custom values if needed. Since we couldn’t find an application that had this exact set of features, we set forth to do it ourselves.

We also wanted to give something back to the community. During our run as a web dev company, we’ve made use of great open source software, and are grateful to the the people who wrote that software. We thought that if we came up with something worth sharing, we would release it under an open source license and return the favor. It was about time that we built and offered something that could make the lives of Scrum teams easier.

So, how does it actually work?

There are two types of users in our application:

Player: These types of users are only allowed to vote, add and modify all of their entries in retrospective sessions. In this way, moderators can have control of the meetings and lead them without interruptions.

Moderator: This is a role commonly used by Scrum Masters. They are allowed to change the description of the story that is being estimated, and clear values to start a new estimation. In retrospective sessions, they are allowed to reveal entries when users finish entering them so that the team can review entries together.

Creating a pointing session:

In order to start pointing you have to create a session, go to the application’s homepage and:

  1. Enter your username
  2. Select your user type
  3. Click “Start”
  4. Optional: modify the names of the pointing values.
  5. Click “Go”!

 

PointingLogin

Creating new pointing session

Setting point values:

You will notice that you can modify the description (names) for points. We included this functionality to give users the freedom to choose the scale and the names for the corresponding elements that works best for them. However, we have also included the most popular pointing range, the Fibonacci series, which is set as default. So, if you don’t need to change anything on the scale, you can move on to the next step of the process without delay. Finally, we added two more features to allow players to express that they are not sure about the estimation of a certain story, or that they need to take a break.

It is important to note that the application doesn’t store those values in memory. They are refreshed when a new session is created, and the only person who can modify them is the creator of the session.

SettingPointValues

Customize point values and labels view

 Joining a pointing session:

Once a user creates a session, she can invite the rest of the team by sharing the link or session ID with them. All users will be prompted to choose a username and user type.

To join with a link:
  1. Click link.
  2. Fill username and user type in prompt.
  3. Click “OK”.

 

EnterUsername

Enter username modal joining session

To join with a session ID:
  1. Go to our home page.
  2. Fill username.
  3. Enter session ID in the corresponding field.
  4. Choose user type.
  5. Click “Join”.

 

JoinSession

Joining a session with an Id

Pointing:

We provide an input to define the description of the story in order to keep all members on the same page. Since the voting process shouldn’t be affected by teammates influencing each other, your vote is secret and you are not allowed to see other teammates’ votes. Accordingly, they won’t see yours until all players are finished voting.

Pointing

Pointing view

Statistics:

The application displays statistics right after all users have finished voting.  Yo’ll be informed of how many players voted the same value. If there was consensus you’ll be notified as well. Votes can’t be changed afterwards.

Statistics

Pointing Statistics

 

Leaving  a session:

Once your team is finished, you can clear the voting area in order to start pointing a different story. Alternatively, if you have finished your sprint planning, you can just close the browser and the session will be deleted.

Creating a Retrospective session:

This process is very similar to the one for creating a pointing session, except that this time we have to select that we are creating a retrospective session instead.

  1. Enter your username.
  2. Select your user type.
  3. Select retrospective from the session type dropdown menu.
  4. Click “Start”.

 

CreateRetrospective

Create retrospective session as Moderator

 

Joining a retrospective session:

Same as if pointing a session: once you create a session, you can invite the rest of the team by sharing the link or session ID with them. All users will be prompted to choose a username and user type as well.

To join with a link:

  1. Click link.
  2. Fill username and user type in prompt.
  3. Click “OK”

 

To join with a session ID

  1. Go to join session section in the home page.
  2. Fill username and user type in prompt.
  3. Enter session Id.
  4. Click “Join”.

 

Adding entries:

Once you enter a session you can see three columns like these:

Retrospective

Retrospective view

 

  1. Click the “+” button on any column.
  2. Write your entry in the box.
  3. Press Enter.

 

EnterMoreOf

More of column in add entry mode

 

WritingMoreOf

Writing an entry for More of column

 

AddedMoreOf

Added entry in More of Column

 

Editing Entries:

Once you add an entry you are allowed to edit it by clicking on the pencil icon.

  1. Click edit icon.
  2. Modify entry.
  3. Click “OK”.

 

 

EditingEntry

Editing an entry modal

 

Deleting Entries:

You are allowed to delete your entries too, just on the trash icon next to the entry.

RetrospectiveEntry

Revealing entries:

Only moderators are allowed to reveal entries, this will show the content of the entries to all users. You can do it by clicking on the “Reveal” button.

Reviewing entries:
ReviewEntry

Preview entry in review mode

 

Once you reveal entries you will be allowed to click on them. That will open a pop up with the text of the entry so you can read the complete entry. There’s an option to mark them as ready or go to the next/previous button.

MarkedAsRead

More of entry marked as read

 

If you are a moderator, you have the option of showing where you are with the rest of the users in the session. With that, you can make sure that all users are looking at the same thing, reading the same entry.

Show for others option

Show for others option

 

Leaving  a session:

This works exactly the same as pointing sessions. When you finish your retrospective meeting, you can just close the browser and the session will be deleted.

How is it built?

We chose Node.js as the main backend platform for this project. We did this because of its “Event Loop” feature and because it does asynchronous operations, which makes our I/O operations fast. But that’s not all, it also features socket support. As you probably know, websockets are two-way communications between the server and the client via TCP instead of HTTP. This means that you can create powerful real-time applications that can push changes immediately. In our case, this came in handy when displaying user votes.

Using Node.js helped us reduce the number of issues that commonly occur when working with multiple languages. Since we had JavaScript already running on the server and the browser, it just made our lives easier. We also used Angular.js. We chose it mostly because of the ‘data-binding’ benefit. Since we were building an application with real-time output, we needed to keep the UI updated, and Angular.js is perfect for that. We’ve been working with this framework for a while, and it makes our development process faster. Plus:

  • It’s maintained by a very friendly and highly-skilled community.
  • It allows for the extension of HTML.
  • It allows for dependency injection.

 

We needed to chose a Javascript library to handle websockets, so we went for Socket.io. We did this for these reasons:

  • It handles browser inconsistencies and varying support levels.
  • It includes additional features like rooms, which allows for work in different teams/rooms/sessions/channels.
  • We think it’s the most reliable real-time engine for Node.js so far.
  • It’s easy to use.

 

And finally, we included Express.js too. We know there are a lot of new frameworks for Node.js out there, but since reliability is of the highest priorities for us, we had to go for Express.js. It has been available for a couple years now and its community and creators are very active. Besides that, this framework is simple and very flexible, and allowed for scalability.

We hope that you found this post interesting. If you have any questions or comments feel free to leave them on the comments section below. Here’s the link to the project on Github. Have a suggestion or found a bug? Send a pull request and we’ll be happy to take a look. We at TangoSource are constantly looking for new and interesting projects that empower our clients and keep the end-user happy. Have something in mind? Send us an email at info@tangosource.com, we’d love to talk 🙂

Thanks for reading!

J.

6 5 Continue Reading →

A Tmux crash course: tips and tweaks.

~ Intro

If you are one of those devs who uses the terminal a lot and ends up with way too many tabs open, or practices pair programming, then this post is for you. During the last months, I’ve started using Tmux a lot. Since I’ve found it to be very useful, I thought I would write a post where I share a few recommendations and pro-tips. I’ll show you what Tmux is and how to use it in combination with Vim to make a more effective and elegant use of the Terminal.

So, this is what we’ll cover:

  1. Tmux basics.
  2. The best of Tmux
    1. Windows
    2. Panes
    3. Sessions
    4. Fast text navigation and copying
    5. And a very neat pair programming feature
  3. Tweaks to improve Vim integration.
    1. Colorscheme background
    2. Static cursor shape
    3. Indentation at pasting
  4. A few extras to enhance the Tmux experience.
    1. Tmuxinator for session automation.
    2. Changing your color of your Tmux bar.

 

An important thing to bear in mind, this is the tool stack I had installed while writing this post, I tested what I say here with these versions:

  • Tmux 1.9a
  • Vim 7.4
  • iTerm 2.1
  • Mac OS (Mavericks and Yosemite)

 

Let’s start!

 

~ The Basics

What is Tmux?

Tmux is a tool that allows running multiple terminal sessions through a single terminal window. It allows you to have terminal sessions running in the background and attach and detach from them as needed, which is very useful. Later on, we will see how to make the most out of that feature.

Here’s a screenshot of a Tmux session:

What you see in the image:

– Left: Vim

– Right: a system’s shell

– Bottom-left: the Tmux session name (“pomodoro-app”)

– Bottom-middle: the current Tmux windows (“app log”, “editor” and “shell”)

– Bottom-right: the current date

How to install Tmux?

In Mac OS:
  1. Install Homebrew

Here’s more info.

  1. Install Tmux:

In Ubuntu:

Run this on the terminal:

The Tmux prefix

In order to isolate its own keyboard shortcuts from other shortcuts, Tmux provides a shortcut prefix. When you want to trigger a Tmux shortcut you will press the Tmux prefix and then the Tmux shortcut key. The prefix that Tmux uses by default is Ctrl-b (“Ctrl” key in combination with the “b” key). For instance, let’s say you want to trigger the shortcut that lists the current Tmux sessions, which is the *s* key. Here is what you will need to do:

  1. Press Ctrl-b keys (Tmux prefix)
  2. Release Ctrl-b keys
  3. Press the s key

 

A few recommendations:

  1. If you have not already mapped the *ctrl* key to the *caps-lock* key and vice-versa I suggest you do it.

 

Calling ctrl from the caps-lock key is very practical. This is because when coding you need to call ctrl very frequently. Moreover, it is a lot easier/quicker given the caps-lock key aligns with the default position of your fingers in the keyboard.

 

  1.  I recommend changing the Tmux prefix to Ctrl-a . Once the  *Ctrl* key has been set to the *caps-lock* key, it gets a lot easier/quicker to call Ctrl-a instead of Ctrl-b, because the new prefix keys are very close to each other on the keyboard.

 

Here is what you need to add in your ~/.tmux.conf file to change the prefix to Ctrl-a:

 The config file

*~/.tmux.conf* is a file that Tmux reads every time a new session opens. It’s where the customizations for Tmux need to be placed. Suggestion: in the case that you need (and chances are you will) to apply a new change made to the without opening a new session, you can add the following line to the ~/.tmux.conf file:

This way, once you have added a new change to the *~/.tmux.conf* file, just press ctrl-b R to reload your configuration without having to open a new Tmux session.

 

~ The best of Tmux

Quick note: the screenshot shown here may differ slightly from what you see by default when you install Tmux. This is because I modified the status bar. If you want to do the same follow the steps on the “Pimp your Tmux bar” section of this post.

Panes

I like the idea of dividing the screen vertically, so that on one side of the screen I have Vim and on the other side I have the output of my code. I could even have another console if I wanted to. Here’s how Tmux makes it happen:

What you see in the image:

– Left side: Vim (at the top: a Ruby class file, at the bottom: a test file for the Ruby class).

– Right side: a bash session.

A vertical *pane* is easy to create. Once you have launched a new Tmux session just press “Ctrl-b %” and a new vertical pane will appear. Also, if what you need is a horizontal division then press “Ctrl-b “”. Navigating through Tmux panes is easy, just press the Tmux prefix and then any of the direction arrows depending on which pane you want to go.

 Windows

In Tmux, a window is a container for one or more panes. Tmux windows allow you to arrange multiple panes inside windows depending on what you need. For instance, in my case I usually have one window called “server” for running the app’s server (where I can see the log), another window called “editor” (where I do the coding). One pane for Vim and another for running the code tests. And another window called “shell”, which is a bash shell where I can run system commands. Tmux windows are useful since they allow users to allocate panes inside of them, to see more about each pane by going to the window that contains it. This is an efficient use of the available screen space.

The list of existent windows in a Tmux session displays at the bottom of the screen. Here is an example of how Tmux displays (by default) the list of windows created. In this case, there are three windows: “server”, “editor” and “shell”):

image3

 

In order to create a new window you need to press “Ctrl-b c”. To navigate through windows press Ctrl-b followed by the index number of the window you want to go. The index number displays next to the name.

Sessions

A Tmux session can contain multiple windows. Sessions are a neat feature; I can create a Tmux session that is exclusive to a particular project. To create a new session just run the following command on your terminal:

If I need to work on a different project I will just create a new session for that. Although the focus will be on the new session, the original session will remain alive. This allows me to get back to it later, and continue where I left off. To create a new session press Ctrl-b : and then enter the following command:

Tmux sessions remain alive until you restart your machine or you explicitly kill the session. As long as you don’t restart your machine, you can jump from a project’s session to another as you need it.

 

Navigation through Tmux sessions

To get a list of the existing sessions, press Ctrl-b s. Here is an example of what Tmux will show you:

image4

Each session listed has an ID number, starting from zero. In order to go to that session type the session’s ID number in your keyboard. In the case that you are not in Tmux but you have one or more sessions running just use:

This command will take you back to your Tmux sessions.

Fast text navigation and copying

I always disliked the fact that to copy content from iTerm2 quickly you need to use the keyboard plus the mouse. I think that there should be a quicker way to do it without having to use the mouse. Fortunately, Tmux allows for that, since it is run from the command line, where the use of the mouse is not allowed.

Navigate text

Tmux allows for text navigation in a way that is very similar to Vim. You know, where the k key goes one line up, w moves one word forwards, and so on. Yet you can increase Tmux’s similarity with Vim by telling it to use the *vi* mode. Here is what you need to add in your *~/.tmux.conf* file to accomplish this:

Send copied text to System’s clipboard

By default, when you copy text from Tmux, the text is only available to be pasted inside that same Tmux session. In order to make that text available to be pasted anywhere, you have to tell Tmux to copy to system’s clipboard. Here’s how to do it:

  1. Install retach-to-user-namespace, this is very easy with brew, just run the following command:

  1. Update ~/.tmux.conf file

Select and copy text

Now that the *vi* mode is set and *rettach-to-user-namespace* installed, let’s see how to copy text from a Tmux session. Let’s say you’ve run the *ifconfig* command because you wanted to copy your ip address. Now, follow these steps to copy that text:

  1. Enter copy mode: ctrl-b [. You’ll see a short highlighted text appear at the top right of the screen as in the following image (“*[0/0]*”).

 

image5

  1. Start moving across the text as you would do in Vim: with j, k, l, h, etc..
  2. Once you get to the text you want to copy press the spacebar and start selecting text (exactly as you would do it in Vim).
  3. Once text is selected press the enter key

 

Now that you have copied your IP address, paste it wherever you want.

Make text copying even more Vim-like

You can use *v* key to select text and *y* to copy it, just add the following to your *~/.tmux.conf* file:

Effective pair programming

You can share the address of a Tmux session with someone else, and that person can connect to the session via *SSH*. Since the connection runs over *SSH*, it will be very lightweight. For the user connecting to the remote Tmux session, it will feel as if the session is running locally, providing the internet connection is fast enough.

Tmux with Tmate

Tmate is a tool that makes it very easy to create a Tmux session and share it with someone else over the internet. To share a new Tmux session using Tmate these are the steps you have to follow:

  1. Install Homebrew:

  1. Install Tmate

  1. Launch a new session with Tmate:

  1. Copy the SSH URL given by Tmate on the Tmux session. An example is showed in the following image (message at the bottom: “*[tmate] Remote session: ssh …*”):

 

image6

  1. Ask the other person to access via *SSH* using the URL you just copied.

 

Now that you know how to make good use of Tmux’s pair programming feature, you can further improve the interactivity of your session by doing a voice call via your preferred provider.

 

~ Tweaks for Vim integration

Colorscheme background

When I first opened Vim through Tmux I found the colors weren’t being correctly applied. The background color was being displayed only where characters appeared. Here is an example:

image7

This issue is due to Vim’s need of setting a different term parameter when ran through Tmux. To set the right term parameter just add the following lines to your *~/.vimrc* file:

After updating the ~/.vimrc file, the color scheme is displayed correctly:

image8

Static cursor shape

By default when Vim is run through Tmux, the cursor shape is always the same regardless of the current Vim mode (insert, visual, etc). This makes it hard to identify the current Vim mode. To fix this, you need Tmux to tell iTerm to update the cursor shape. You can do it by adding the following to your ~/.vimrc file:

*Thanks to Andy Fowler who originally shared this tip.*

Indentation at pasting

Sometimes, when pasting text into Vim the indentation of the text is changed, which is a problem when you paste a large amount of text. This issue can be prevented by executing :set nopaste before pasting. However, there is a better way to do this. By adding the following to your ~/.vimrc file, Vim will automatically prevent auto-indenting the text when pasting:

*Thanks to Marcin Kulik who originally shared this tip.*

 

~ Nice extras

Tmuxinator (automate sessions for your projects)

Let’s say you start coding for application A and you always create a Tmux session with three windows for that: “servers”, “editor” (for the project’s code) and “shell” (to run system commands). At certain point of the day you need to temporarily switch to application B, and you will create a Tmux session with the same windows arrangement you had for application A, with a few slight differences (such as the directory and some commands). With Tmuxinator, you can declare the configuration for each Tmux session and then create them with a single command! Pretty sweet.

Tmuxinator is a gem that allows you to automate the creation of Tmux sessions. It is done specifying the details of the sessions in the configuration files and then creating the sessions with a command.

Let’s see how to install Tmuxinator and how to add the configuration to start a project session. Install the Tmuxinator gem by running the following command:

Now that Tmuxinator is installed you can run the tmuxinator or mux commands from the system’s shell. Let’s create the configuration file for your first application as described above (three windows: “servers”, “editor” and “shell”), tell Tmuxinator to create and open the config file for this project:

At this point the file ~/.tmuxinator/project_a.yml should have been automatically opened. In order to accomplish what is needed for project A you need to update the content of project_a.yml to:

Once you have added the configuration to the Yaml file for project A, just run the following command to start the Tmux session:

Or, if you prefer, use the Tmuxinator alias:

There you have it. Now, in order to start coding for project A just run the Tmuxinator command.

*Find the official documentation in the Tmuxinator repo.

Pimp your Tmux bar

By default, the Tmux bar looks like the following (green bar at the bottom of the image):

image9

You can change its appearance if you want. In my case I like something cleaner as in:

image10

In order to accomplish it, I use the following settings in my ~/.tmux.conf file:

~ Conclusion

In summary, we revised basic functionality and the most useful features Tmux offers. We also reviewed a few tweaks and extras. What are your impressions about Tmux so far? Are there any other features/tweaks that you have found useful? Let us know in the comments section below.

Thanks for reading!

TN.

12 28 Continue Reading →

HTML5 Canvas: playing with shapes and images.

The World Wide Web Consortium (WWC) introduced Canvas with HTML5 last year. We think it’s a pretty interesting drawing tool. So, we decided to experiment with it and share our findings in the form of a short tutorial in this blog post. We’ll cover the basics and mention a couple of recommendations for deeper learning.

To start understanding Canvas, think of it as an area on your page where you can draw and render graphs. It’s user together with Javascript, taking advantage of its properties and methods. Its purpose is to be a container for graphics, as it has no drawing abilities on its own. It may seem like a simple element, however, it can be used for complex tasks, like creating 2D animations. You can see examples of what Canvas can do here. Most modern browsers support Canvas, such as Chrome, Firefox, Safari, Opera, and Internet Explorer 8 and later.

Getting Started

In order to start drawing we need to add the Canvas element to our page, we’ll do it with this code:

<Canvas id="Canvas" width="800" height="480"> Your browser does not support HTML5 Canvas</Canvas>

As you may notice, we defined a width and height for this element. If you don’t define the size, it will use the default values, which are 300x150px. Inside the Canvas tag, you can write a message to be displayed if the browser does not support it.

1

The Canvas element is displayed in light blue.

Once we have added our Canvas element, we’ll use Javascript to get the “context”. You can insert Javascript code in the HTML header, a script block or even in another file. The code should look like the following:

var d_Canvas = document.getElementById('Canvas');
var context = d_Canvas.getContext('2d');

And now we are ready to draw!!!

We’ll start with the basic forms and shapes:

Text

In order to show text in our element, we need to take care of three things: font, fillStyle, and fillText. More specifically, this is what you should do:

– Define size and font type in the “context.font” property.
– Define text color in the “fillStyle” property.
– Use the “fillText” function as in the example below. The first parameter is for the text to show, and the next two are the X and Y axes, respectively.

context.font = "30px Arial";
context.fillStyle="#000";
context.fillText("Hello, World! This is a text", 20, 100);

2

Canvas now displays a gray color.

Square

To draw a square or a rectangle we use “fillRect”. Once again, we are defining the color of our square with the “fillStyle” function. When using “fillRect”, we have to include the following parameters: (positionX, positionY, width, height).

Example:

context.fillStyle = 'red';
context.fillRect (175, 75, 50, 50);

3

And this is what we get from Canvas.

Circle

To draw a circle we use “beginPath” first and then “arc”. First draw the outline and then call the “fill” function. The code should look like this:

context.beginPath();
context.fillStyle = 'green';
context.arc(200, 100, 50, 0, Math.PI * 2);
context.fill();

5

And this is the result.

The “arc()” function takes the following parameters: (x, y, radius, starting angle, ending angle, anticlockwise). As you may notice, in this example we didn’t use the anticlockwise parameter, as it is optional, and it wouldn’t have changed the result.

If you’re wondering about the meaning of those parameters: here’s what they mean]:

Starting angle: angle in radians to start drawing the outline (0 will be at the three o’clock position)

Ending angle: the ending angle in radians.

CounterClockWise: boolean value that draws anticlockwise when true, and clockwise when false.

Gradients

We can fill a shape with a color gradient, which means that a filling can start using one color and end with another. To do this in Canvas, we create a gradient using the “createRadialGradient” method, in which we specify two circles. The first circle is where the color is going to begin changing, and the second one tells Canvas where the color gradient is going to end. To declare every circle, we pass the X and Y coordinates of the circle’s center and radius to the “arc” function. Take note, the gradient could use other fillings if needed.

var grd = context.createRadialGradient(190, 60, 15, 190, 80, 50); //x1, y1, radius1, x2, y2, radius2.

Once we have declared the reach of the gradient, the next step is to set the gradient’s colors. We do this by using the “addColorStop” method, which requires two parameters. The first one is the “stop” or the point in which the color will begin. Stops go from 0 to 1, so make sure you distribute your colors between those numbers. The second parameter is the color we want to paint on that stop.

grd.addColorStop(0, '#F00');
grd.addColorStop(.2, '#FF0');
grd.addColorStop(.4, '#0F0');
grd.addColorStop(.6, '#0FF');
grd.addColorStop(1, '#00F');

To assign our new gradient to our shape we use the “fillStyle” method to tell the context that its filling is going to be the gradient we just built. Finally, we invoke the “fill” method to tell the context to fill the shape with the gradient.

Example:

context.beginPath();
context.arc(200, 100, 35, 0, Math.PI * 2); // A regular circle figure.
context.fillStyle = grd
context.fill();

6

And you’ll get this result.

Ellipse

For ellipses, use the “ellipse()” function, which requires the following parameters: (x, y, radiusX, radiusY, rotation, start angle, ending angle, anticlockwise). The rotation parameter indicates how much will the ellipse turn on the axis. In radians, as usual.

Here’s an example:

context.beginPath();
context.ellipse(200, 100, 50, 20, 0, 0, Math.PI*2);
context.stroke();

7

And this is how it would look.

Line

We use the “beginPath” function to declare that we are about to draw a new line path. Next, we use the “moveTo” function to state the starting position (imagine that you are moving a cursor). And then we use “lineTo” to draw a straight line from the starting position to the ending position. And finally, we need to call stroke() to “paint” through the covered path.

Example:

context.beginPath();
context.moveTo(75, 50);
context.lineTo(350, 180);
context.stroke();

8

The result.

A pretty cool thing we can do with Canvas is to render an image. We can draw an image directly from an existing image element within the HTML document or use an external picture.

In order to use an internal image element, we have to include the image element with a specific ID inside the document.

<code><img id=”dog” src=”http://3.bp.blogspot.com/-lCei4CIR7J8/UOXuPWztz5I/AAAAAAAAAAw/iNkkVQzUV88/s1600/cute_little_dog_wallpaper-1024×1024.jpg” alt=”Dog” width=”100″ height=”110″></code>

Then, we get the image element by its ID using Javascript, and use the “drawImage” method to render the image in the canvas:

var img = document.getElementById("dog");
context.drawImage(img, 110, 0, 180, 200);

In this case, the “drawImage” method has the image element as its first parameter, then the X and Y coordinates, and finally the width and height of the image in the canvas. Also, you can see that the original HTML element is visible, so it is recommended to hide it.

9

This is the result.

But, if we want to use a picture that is outside of our HTML document, then we have to create an object of the “Image” type first. Then we can assign it an external source path. We invoke the drawing method on the “onload” event of the image so the picture can draw itself the moment it’s fully loaded. If we don’t do this, the drawing method may fail because the picture won’t load when the drawing method is invoked.

var image = new Image();
image.src = "http://www.mycatspace.com/wp-content/uploads/2013/08/adopting-a-cat.jpg";
image.onload = function(){
context.drawImage(image, 100, 0, 200, 200);
};

10

This is what you get with the code above.

With Canvas, there are no limits to your creativity, just let it flow…

If you want to avoid repetitive tasks, there are some frameworks that can help you speed up your work. Some of them specialize in picture handling and others in making charts or creating filters and effects. Take a look at FabricJS, ChartJS and PaperJS if you want to explore these possibilities.

Another great feature of Canvas is that you can create graphic content in real time, and even respond to user events. A couple of final tips: remember that in order to start/reset a path you need to call the “BeginPath()” function. Here’s a pretty useful cheat sheet with frequently-needed code, and a live example you can use for experimentation purposes.

We hope you found this post useful! Let us know what you think in the comments section.

Thanks for reading!

J.

0 10 Continue Reading →

Don’t let working remotely ruins your performance

Certainly, the concept of ‘remote work’ sounds very tempting…

Ill

(Photo credit: Jan Kaláb)

… but don’t let that fool you! Working remotely is a double-edged sword.

I have found three main areas you need to take care of if you want to stay productive while working remotely: discipline, communication, and time zones. Keep reading to see why I am saying this, plus some tips that will prevent you from performing poorly. And who knows, perhaps you’ll end up having a better performance working remotely than if working onsite.

Discipline

I remember the first time I worked remotely, it felt awesome! I could start or pause work at any time I wanted. And that feeling of freedom, of being able to go to the kitchen and grab some food, watch some TV if I got bored… it was great. I used to wake up at 9:00am, have breakfast, take a shower, and then start work at 10:00am. At this point I was running an hour late, if we compare it with a typical office schedule.

It didn’t take long before I found myself working the whole day. Starting at 10:00am, making frequent breaks, plus doing all the chores and errands a normal person in his twenties has to do. The result: I ended my work days at 12:00am… I had turned into a slave of the freedom that I had.

The solution

You need to take remote work as seriously as if you were working onsite. Remember, with so much freedom it might not seem like it is the same, but you still have have a job to do!

Obviously, taking breaks once in a while does not hurt, but I recommend you avoid granting yourself too much freedom until you have mastered your procrastinating nature. It’s possible to tell if your impulses are under control after a couple of months of working on it, tracking how successful you have become at achieving your goals. And yes, you read right, it takes months. It has been proved that it takes over 2 months of daily practice to develop and establish a new habit.

If it is very hard for you to be disciplined, there are some tools available out there that could help you keep focused on work. A very popular (and effective) one is the pomodoro technique. This time-management method can help you improve your focus and mental agility. Pomodoro promotes work intervals that last 25 minutes, and are alternated with 5-minute breaks, thus increasing your productivity. I have tried it myself and it works, it structures your work day in a way that makes you spend your time more efficiently.Try it out yourself and see if it brings any positive changes to your productivity.

Communication

When I started working remotely I was in charge of a team of 3 people. As you can imagine, not only was my own performance compromised, but also that of my team and project’s. Unread emails and messages were something I had to deal with every day; slowly but surely this was something that was affecting our work deliveries.

The solution

Remember, when you work remotely, you aren’t really alone. You have co-workers, clients, supervisors and other people you work with. Because of this, it is very important to share your status frequently. You can decide on different strategies with your teammates. These are some I personally prefer:

1. Email at the beginning and at the end of the day. I recommend this specially to improve communication with your client. Generally, they are very busy during the day because they have very tight schedules. A good practice I like keeping is sharing my work plan for the day with the client, using the following Scrum-based format:

  • What I accomplished since yesterday:
    • goal 1
    • goal 2
    • goal n
  • What I’m planning to accomplish today:
    • goal 1
    • goal 2
    • goal n
  • Impediments:
    • list the stuff that prevented you from achieving a goal

 

2. Use text messengers appropriately. Use them concisely and only when communicating important stuff to your teammates, you don’t want to interrupt them frequently as you don’t want to be interrupted often either. Here are some examples of things worth communicating and how to do it briefly:

  • ‘A bug appeared in production…’,
  • ‘I finished the feature…’
  • ‘I would like to know the status of…’
  • Or simply ‘I am leaving!’, so everyone is aware you won’t be available from that moment on.

 

3. Keep project management tools updated. Some PM tools allow the addition of comments inside stories (also known as tickets). I recommend you to keep your stories updated by adding a comment with an status whenever any of the following happens:

  • You hit a milestone while building a feature.
  • You have made progress solving a bug.
  • You are going to perform a chore.
  • You are stuck.

 

This way, the client and your teammates know what’s the progress on that story. You will help others to determine if the committed work is in risk of not being delivered, and you can receive help to speed up your work.

Beware of different time zones

This is perhaps the least harmful of the three, because it does not affect your work directly, but it can be a rock in the shoe. I didn’t know this was going to be a constant pain until I started working remotely. I had to deal with 3 different time zones: our customer was on PST, my co-workers on CST, and I was on EST. I was 3 hours ahead of our customer and 1 from my co-workers, and this brought communication issues. When any of them tried to contact me it was very likely that I had already finished my work day.

The solution

Be sensible when arranging meetings, inform others the time when you usually have lunch. Let them know at least one week in advance if you are going to take days off (including those you take for religious reasons, if that’s your case). It took me about 4 weeks to get used to all time zones and to adjust my personal schedule, sometimes having to move my lunch time an hour earlier or later, but at the end it was worth it.

Other good practices

Here at TangoSource we are proud of our remote work practices. We summarized best practices for remote work on our playbook so that anyone can work remotely effectively. Here are a couple of examples:

  1. Create a daily routine list. This list contains recurring action items that you will be checking at the beginning, during, and at the end of the day. An example of a daily routine is: checking and answering emails at the beginning of the day, check messenger apps during the day, and sharing your status at the end of the day.
  2. Create a weekly routine list. This list contains all action items that you will need to take care of during the week. Example: meetings, days off, deployments to servers, etc. Having this routine will help you to have a well-organized schedule.

The conclusion

It took me four to five months to master the three areas mentioned above. If you’re struggling to deliver results while working remotely, or if you’ve never done it and are afraid to try, well, now you know what to do. Put these ideas in practice, don’t give up, and soon you’ll be ready to work from the Caribbean!

Are you interested in learning more about TangoSource and having us as part of your development team? Shoot us an email! We would absolutely love to hear from you! 🙂

by Marco Gallardo: Email / Github

2 8 Continue Reading →