Tag Archives: coding

Json-api-normalizer: Why JSON API and Redux Work Best When Used Together

As a web developer, we have to manage the data needed for every application we work on. There are problems when doing so, such as:

  1. Fetch data from the back end.
  2. Store it somewhere locally in the front-end application.
  3. Retrieve the data from the local store and format it as needed by the specific view or screen.

In this article, we are going to discuss about the data usage from JSON, the JSON API and GraphQL back ends, and from that, we can learn the practical way on how to manage front-end application data. As for the real use, let’s imagine that we have carried out a survey that asks the same questions of many users. After each user has given their answers, other users can comment on them if wanted to. Our web app will perform a request to the back end, store the gathered data in the local store and render the content on the page. In order to make it stay simple, we will leave out the answer-creation flow.

Redux Best Practices

What makes Redux the best is that it is changeable no matter what kind of API you consume. It doesn’t matter whether you change your API from JSON to JSON API or even GraphQL and back during development, as long as you keep your data model, so it will not affect the implementation of your state management. Below is the explanation on the best practice using Redux:

  1. Keep Data Flat in the Redux Store

First, here’s the data model:

 

 

Based on the picture above, we have a question data object that might have many post objects. It is possible that each post might have many comment objects. Each post and comment has respectively one author.

Let’s say we have a back end that returns a specific JSON response. It is possible that it would have a carefully nested structure. If you store your data in the same way you do in the store, you will face many problems after that, like, for instance, you might store the same object many times like this:

{

  “text”: “My Post”,

  “author”: {

    “name”: “Yury”,

    “avatar”: “avatar1.png”

  },

  “comments”: [

    {

      “text”: “Awesome Comment”,

      “author”: {

            “name”: “Yury”,

        “avatar”: “avatar1.png”

      }

    }

  ]

}

In the example above, it indicates that we store the same Author object in several places, which is bad, because not only does it need more memory but it also has negative side effects. You would have to pass the whole state and update all instances of the same object especially if somebody changed the user’s avatar in the back end.

To prevent something like that from happening, we can store the data in a flattened structure. This way, each object would be stored only once and would be easily accessible.

{

  “post”: [{

    “id”: 1,

    “text”: “My Post”,

    “author”: { “id”: 1 },

    “comments”: [ { “id”: 1 } ]

  }],

  “comment”: [{

    “id”: 1,

    “text”: “Awesome Comment”

  }],

  “author”: [{

    “name”: “Yury”,

    “avatar”: “avatar1.png”,

    “id”: 1

  }]

 }

  1. Store Collections as Maps Whenever Possible

After we have the data in a good flat structure, we can gradually accumulate the received data, in order for us to reuse it as a cache, to improve performance or for offline use. However, if we combine new data in the existing storage, we have to select only relevant data objects for the specific view. We can store the structure of each JSON document separately to find out which data objects were provided in a specific request to gain this. There is a list of data object IDs that we can use to gather the data from the storage.

Let’s say there is a list of friends of two different users, Alice and Bob. We will then perform two requests to gather the list and review the contents of our storage consequently. Let’s suppose that from the start the storage is empty.

/ALICE/FRIENDS RESPONSE

Here’s the User data object with an ID of 1 and a name, Mike, like this:

{

  “data”: [{

    “type”: “User”,

    “id”: “1”,

    “attributes”: {

      “name”: “Mike”

    }

  }]

}

/BOB/FRIENDS RESPONSE

This is another request that would return a User with the ID of 2 and Kevin as the name:

{

  “data”: [{

    “type”: “User”,

    “id”: “2”,

    “attributes”: {

      “name”: “Kevin”

    }

  }]

}

STORAGE STATE

This is what our storage state would look like:

{

  “users”: [

    {

      “id”: “1”,

      “name”: “Mike”

    },

    {

        “id”: “2”,

        “name”: “Kevin”

    }

  ]

}

STORAGE STATE WITH META DATA

In order to find out or distinguish which data objects in storage are relevant, we have to keep the structure of the JSON API document. With that focus, we can change it into this:

{

  “users”: [

    {

      “id”: “1”,

      “name”: “Mike”

    },

    {

        “id”: “2”,

        “name”: “Kevin”

    }

  ],

  “meta”: {

      “/alice/friends”: [

        {

          “type”: “User”,

          “id”: “1”

        }

      ],

      “/bob/friends”: [

        {

          “type”: “User”,

          “id”: “2”

        }

      ]

  }

}

With this, we can now read the meta data and gather all mentioned data objects. Now here’s the recap of the operations’ complexities:

As can be seen from the picture above, maps certainly works better than arrays because all operations have O(1) as the complexity instead of O(n). If we use a map instead of an array for the User data object, it would be like this:

STORAGE STATE REVISED

{

  “users”: {

      “1”: {

        “name”: “Mike”

      },

      “2”: {

        “name”: “Kevin”

      }

  },

  “meta”: {

      “/alice/friends”: [

        {

          “type”: “User”,

          “id”: “1”

        }

      ],

      “/bob/friends”: [

        {

          “type”: “User”,

           “id”: “2”

        }

      ]

  }

}

Now with this simple method, we can find a specific user by ID almost instantly.

Processing the Data and JSON API

There are many solutions to convert JSON documents to a Redux-friendly form. However, while there is no significant change within the application’s lifecycle, it will cause a failure if things are too dynamic, even though normalizing the function with the provision of a JSON document works great if your data model is known in advance.

Using GraphQL might be possible and interesting as well; however, if our APIs are being consumed by many third parties, we can’t adopt it.

JSON API and Redux

Redux and the JSON API work best together. The data provided by the JSON API in a flat structure by definition conforms nicely with Redux best practices. The data is typified in order to be naturally saved in Redux’s storage in a map with the format type → map of objects.

There are things to consider, though. First, it should be noted that storing the types of data objects “data” and “included” as two separate entitles in the Redux store can violate Redux best practices, as the same data objects would be stored more than once.

To solve these problems, we can use the main features of json-api-normalizer, such as:

  • Merge data and included fields, normalizing the data.
  • Collections are converted into maps in a form of a id=> object.
  • The response’s original structure is stored in a special meta

First, in order to solve problems with redundant structures and circular dependencies, the introduction of the distinction of data and included data objects in the JSON API specification is needed. Second, there is a constant update on data in Redux, although gradually, that can help with the performance improvement.

 Now that you know why JSON API works best with Redux, it can be concluded that this approach can assist us in prototyping a lot faster and flexible with changes to the data model. If you are in doubt whether using Redux with JSON API or not, this article will help you find the solution and reason why you shouldn’t doubt this method.

Predictions of IPv6 in 2017

IPv6-Predictions-for-2017_ywf

If you are a web developer, you might have  an experience with IPv6. You might find it either enticing or loathing at the same time. However, many developers discover that 2016 is a great year for IPv6, so it is no longer ramp-up, as using IPv6 advocates have often been frustrating by the pace of adoption. The good news for us was that 2016 was a really great year for IPv6. To discover how many changes that you can find in this new IPv6, you can take a look on the information below:

In a similar format to our IPv6 predictions for 2016, we are simply stating what we think will happen.

  1. The majority of container solutions (Docker, Kubernetes, Mesos) will have IPv6 support by the end of 2017
  2. IPv6 growth worldwide will, again, outpace the US
  3. Major private cloud solutions (OpenStack, AzureStack, VMware) will have production ready IPv6 support
  4. Security will finally start figuring out IPv6
  5. Early IPv6-only data center solutions will start happening

It is predicted in 2017; there will be more developers adopting IPv6 at a faster rate as containers and those that have solutions around containers will continue seeing the massive growth. Containers will become the next generation of operator platforms replacing VMware vCenter or OpenStack Horizon. Many developers believe that this is the solution to run and operate with IPv4 and/or IPv6. It is because more than 33% of native IPv6 services (mainly due to mobile operators) and the rate will grow steadily over 2017. However, since there are still so many countries outside the US which have not had high adoption rates, they have a much higher initial deployment growth curve to leverage. A massive deployment of IPv6 will appear almost overnight, when a single service provider enabling IPv6 for a country. Moreover, other countries, such as China or Russia are also poised to do just that in 2017.

As more and more customers determine that an all-in public cloud strategy does not address all their business requirements or concerns,  you will see an uptick in hybrid-cloud solutions that will require deployment of private clouds. To allow low friction utilization of both public and private clouds, these private clouds will have to be as tightly integrated with their public cloud counterparts. Some of us may have noticed that both AWS and Azure have native IPv6 capabilities and hopefully, Google will be the same.

Furthermore, you will not only see IPv6 specific capabilities within security product portfolio, but also event correlation and matching for dual-stack hosts. Therefore, it is important to understand the relationship between IPv4 and IPv6 and what kinds of features or events are happening. In the end, developers will no longer opt to turn off IPv6 as the standard request from IT security and gain skill and insight into what IPv6 is doing. As a result, stakeholders will become more common with what IPv6 is doing. In fact, the craziest prediction is that many big companies will take a serious look into the option of doing an IPv6-only solution to meet their primary customer needs. To keep providing resources for an IPV4-only host, developers may adopt protocol conversion or proxy functions for IPv4 with IPv6. In addition, compared with a dual-stack, it will be far more cost-effective to deploy and operate a new data center with IPv6

In conclusion, IPv6 will surely become an important part of data center story in 2017. Others, like cloud, containers and global adoption will end up as the big IPv6 stories.

 

An Excellent Guide to Choosing a WordPress Theme

A-No-Nonsense-Guide-to-Choosing-a-WordPress-Theme_ywf

As a web developer, you will certainly notice that there are more than 10,000 WordPress themes currently. These amounts will confuse you, so you should know how to pick one that is best for you, right. In the following information, you can learn how to select the best theme for you. Therefore, make sure you read all of the following information.

Free Themes – Pros and Cons

Nothing wrong with free themes, they don’t necessarily have any huge disadvantages over paid themes. You don’t have to pay for any advertising on your site or branding. If you find that the free theme can fulfil your need, go for it, you can always upgrade it to premium later.

Pros:

  • Free
  • High security and good quality coding if you choose a free themen from the official WordPress directory
  • Often simple, lightweight, and easy to set up and use.

Cons:

  • Lots of people use the same themes so your site may not look unique
  • Limited customization options
  • Limited support
  • Fewer updated than premium themes

Premium Themes – Pros and Cons

Generally, big sites or company will use premium themes, if you’re setting up a site for your business, you have a blog with a large following that you want to move to WordPress, or if you want to make any kind of income from your site, you might want to think about choosing premium from the start. Moreover, having a premium theme also will give you a unique theme. However, this doesn’t mean that a premium theme is always better than a free theme. You can still find some badly coded and slow to load premium themes out there.

The price of a premium theme varies from just a few dollars to several hundred. Besides, high price doesn’t guarantee the quality. Therefore, you have to be smart in choosing the right features of your theme.

Pros:

  • More features
  • Unique design
  • More frequent updates
  • Advanced customization options
  • Developer support.

Cons:

  • Not always more secure or better coded than free themes
  • Some premium themes are bloated and slow to load
  • Not free
  • Most premium themes ask for a recurring payment to keep receiving support and updates.

After knowing the pros and cons between free and premium theme, now it’s time to choose your WordPress theme design. Here is some guidance for choosing the right theme.

  1. Fit for Purpose

When choosing for a theme, it is not for beauty only, but the main purpose of your site is also important. For example, if your content is mostly text then you’ll want to choose a theme that’s designed for a good reading experience. However, if you want to display more images with a focus on visual content don’t choose an image-heavy design, although it may look pretty.

  1. The Importance of Responsive Design

As so many people are using smart phones and other small screen devices to access the web, it is necessary to use a fully responsive theme. Make sure you’re happy with how the site looks and works on mobile as well as full screen.

  1. Keep it Clean and Simple

To set a style that will be suitable for a long time, you better choose a simple design that can showcase your content. In fact, overly designed themes go out of fashion very quickly.

Instead of focusing on the images, it is also important to look at the layout and spacing of the site. Some themes may look amazing in preview, but when you substitute in your own images, they’re not so impressive.

  1. Theme Features and Plugin Compatibility

Another aspect is to consider the additional functionality of the theme. Many premium themes come with all sorts of bells and whistles, which may or may not be useful for your personal needs. Besides, premium themes tend to have more advanced and extensive features. Some common features of premium themes include:

  • Image or featured posts slider
  • Social sharing icons, menus, and widgets
  • Custom page styles (different layouts for blog, gallery, about, etc.)
  • Mega menus
  • Retina-ready (theme looks crisper and more detailed on HD displays)
  • Custom shortcodes (to easily add buttons, styled links, icons and other design elements to your site posts and pages)
  • Ecommerce features
  • Translation ready

But, be careful when choosing a theme with too many features. You can usually use a separate plugin to do the same job, so never base your choice on features alone.

  1. SEO

We cannot neglect the SEO standards which will make your web easily to be searched for when someone searched for a related term. Therefore, make sure that your site is marked up properly with meaning using the correct code for page headings, image descriptions, etc.) And that it loads quickly. To find theme that is SEO friendly, you can do your own research and read some real reviews from people’s experience.

All the above tips lead you to one thing that you should know which theme that has a good quality and suitable for your needs.