James Huynh My little tech blog

How to use graphql gem in Rails with ReactJS

In this article, let’s explore on how to use GraphQL implementation with Rails and ReactJS to build a simple app which would do the followings:

  • Login with an email & password using graphql
  • List all books with title, author name & number of comments

We would also explore how to deal with N+1 query problem with GraphQL

Download Exported CSV in the background with Action Cable and FileSaver.js

I set a rule in our company that we cannot have a long running process running in Rails controller as it will easily prolong the occupation of one of the puma/unicorn server instances. We usually have a limited number of puma/unicorn server instances and it is a bad idea of having one of them occupied for more than 1 second, as the more and the long time the puma/unicorn server instance is occupied, the less throughput we would have. This could result in slow site and bad user’s experience.

One of the common tasks that can occupy the puma/unicorn server instance for a very long time is generating a CSV report task. The usual process is that a user would click on a generate CSV file button and wait there until the file is fully generated on the server and then trigger a download through Rails send_data command in a controller. In this article, I am going through on how we can solve this problem by making the report generation happen in the background and only trigger the download once it is finished generating on the server.

Deploy NextJS app with Capistrano 3, NVM and PM2 on Ubuntu server

I was recently tasked to deploy a NextJS app into a ubuntu server. The requirement was that the deployment needs to be fast and as reliable as possible. Although I have been using mina for some of the apps, mina still does not really support multiple server instances deployment, which is quite a disappointment. Luckily, capistrano 3 supports multiple server deployment out of the box and pretty fast, reliable to be chosen as a deployment tool for all our projects. In this article, I am going to go through how I use Capistrano 3, Node Version Manager (NVM) and PM2 (a Node JS process manager) to deploy reliably a NextJS project.

How to paginate Rails find_by_sql result

For Rails developers, we are all familiar with pagination. I am using and happy with Kaminari to do pagination. However, there is a problem, quite a serious problem with Kaminari is that I could not use Kaminari to perform pagination on the famous find_by_sql function as find_by_sql simply returns a Ruby array while Kaminari requires an ActiveRecord::Relation in order to append the pagination query portion. In this article, let’s go through how I still can do pagination on find_by_sql without using Kaminari.

How to upload files using React and Rails like a boss

Handle file upload in React is not a trivial topic, especially with multiple files upload. In this article, I would like to go in depth how we can make use of React, FormData to upload to Rails smoothly. The features that we are going to build in this article will include

  • Single/Multiple File Upload.
  • Accept certain file types.
  • Show progress bar when files are being uploaded
  • Preview the files before they are uploaded
  • How Rails can handle to multiple file uploads

Generate Free SSL Certificate for your domain name

I have been purchasing SSL for my client applications using various service provider like Trustico, RapidSSL. However, the process of generating the SSL is quite a lengthy, time consuming and troublesome process. Recently, I found out that, we can actually generate SSL from a service provider - Let’s Encrypt, a service that lets you generate free SSL certificate for free without a lot of hassle like a common SSL service provider. In this article, I am going to go through how to use this to generate an SSL certificate for one of my domain name.

Perform cross domain request with xdomain

We use a lot of cross domain requests from ReactJS app to Rails. At the moment, in order to do that, we would need to use rack-cors, a gem to let Rails app to allow cross domain requests from javascript. I discovered xdomain recently and recognise that althought it is relatively a new tool to help to achieve the same thing but with simpler setup. In this article, I am going through on how we are going to setup and use this tool.

How to effectively cache json in API Rails app

It is quite a trend now that we use Rails API combining with a front end framework like ReactJS, AngularJS or VueJS. This is also a good way for us to streamline the data serving. All data flowing out from Rails app will just be JSON. This sparks my huge interest of finding a way to cache the returned json so that it can return faster without hitting the database. In this article, I would like to share a way on how to do effective cache json.

Restore remote MySQL Database into your local development in Rails

Since the day I started deploying to a remote server until recently, I have always had to load database from staging server or production server into my local Rails development environment for some debugging purposes. However, it is not a simple process and usually occupy some of my times with quite a bit of manual commands.

Recently, I have decided this have to be changed and that moment I have started writing a Ruby gem just to do this mundate task.

How to maximise using Regular Expression in Ruby

Regular Expression can be overlooked by Ruby developer sometimes. However, if we know how to make use Regular Expression, it could be extremely useful and save a lot of our time dealing with ruby strings problem. In this article, I am going through some basic knowledge of Regular Expression in Ruby and how we can fully utilise it for our benefit.