ElectronJS + Vue.js: Setting up ipcRenderer event handlers

When utilizing the ipcRenderer within a Vue.js view component, make sure you register the IPC event handler once for that component using ‘ipcRenderer.on’ in the ‘mounted’ lifecycle event of the Vue.js component.

If you make the mistake of placing the IPC event handler registration (ipcRenderer.on) inside a function that is called more than once within your component, you will notice a strange side-effect. What you will end up experiencing is that each time your function is called, the ipcRenderer will register a new event handler for the named event and each time that event is called you will see the handler executed X-number of times, with X equal to the number of times the function that contains ‘ipcRenderer.on’ has been called within your component.

If you are not careful, this is an easy mistake to make and a time consuming one to figure out.


 mounted: function () {
    this.$nextTick(function () {
        //Register IPC Renderer event handles once for this control
        ipcRenderer.on('name-of-your-event', (e, args) {
            // TODO: Hanlde the event broadcast here
        });
    });
}

Vue.js: Adding Keyboard Shortcuts to Components

A simple method for adding keyboard shortcuts to your components is to do the following:

    1. Add the ‘mounted’ lifecycle event hook to your component
    2. Add an event listener to the ‘window’ object and have it listen for the ‘keyup’ event
    3. Check for the proper keyboard key code and system modifier key (command/control, option/alt, shift, etc.) combination
    4. Execute custom logic in response

Example: here the key combination ‘alt+n’ is being checked for

 mounted: function () {
        this.$nextTick(function () {
            window.addEventListener('keyup', event => {
                if(event.altKey && event.keyCode === 78){
                    //TODO: do/call something here
                }
            })
        });
    }

Electron JS: Handle CMD+Q in OS X

When creating an Electron desktop application you will find that it does not, out of the box, support the traditional keyboard shortcut CMD+Q to close your application when running on OS X. To solve this situation, you simply need to import the ‘globalShortcut’ object from Electron, check for OS X ‘Darwin’, add a listener for the key combination, and when that combination is hit quit the application.

If you are using the Electron template code for your Main.js file (might be different if you are using custom code, but you can figure it out easy), here is what you need to do:

  1. Add ‘globalShortcut’ to your electron library import statement
const {app, BrowserWindow, Menu, globalShortcut} = require('electron');
  1. In your ‘createWindow’ function, add the following code to check for OS X ‘Darwin’ and add a listener to the CMD+Q key combination that executes the ‘quit’ function on the application
if (process.platform === 'darwin') {
    globalShortcut.register('Command+Q', () => {
        app.quit();
    })
}

Ubuntu + Rmagick + Gem

 

If you run into trouble installing Rmagick on Ubuntu, it is most likely because you do not have the dev libraries installed that are needed by the rmagick Ruby Gem. Run the following commands in your terminal and everything should work fine.

sudo apt-get install graphicsmagick-libmagick-dev-compat
sudo apt-get install imagemagick
sudo apt-get install libmagickcore-dev
sudo apt-get install libmagickwand-dev
gem install rmagick

PostgreSQL + rake db:migrate

ActiveRecord::NoDatabaseError: FATAL: role “dev” does not exist

If you receive the error above when trying to run your Rails migrations against a newly installed instance of PostgreSQL on an Ubuntu development box, all you need to do is a new role to Postgres with the name ‘dev’. Once the role has been added and given the proper permissions, your migrations should run without issue.

 

Ubuntu + RVM: Ruby Install – There was an error(23)

After a new install of RVM on Ubuntu, you may run into the error “There was an error(23)” in conjunction with a file write or folder creation permission error. This is caused when, for some reason, RVM does not properly add your Ubuntu user to the RVM group after it was installed. To correct this error, you need to complete the following steps, and executing the following commands:

Execute in terminal:
rvm group add rvm "$USER"

Log out of Ubuntu and log back in. If it still fails, you may need to have RVM fix its system permissions with the command:

Execute in terminal:
rvm fix-permissions

PostgreSQL Database: PG::ConnectionBad: FATAL: Peer authentication failed for user

If, when working on a Linux machine of the Ubuntu flavor and setting up a PostgreSQL 9.3 database, you run into the error ‘PG::ConnectionBad: FATAL: Peer authentication failed for user’ when trying to connect to a database from a web application (Rails, PHP, Node, etc.), you are more than likely running into local socket connection permissions within PostgreSQL. The most common fix for this error in a development or staging environment is to loosen the local permissions up a bit.

How To:
1. locate the file ‘/etc/postgresql/9.3/main/pg_hba.conf’ and open it using sudo (sudo nano /etc/postgresql/9.3/main/pg_hba.conf)
2. scroll down through the file (almost to the bottom) until you find the section that starts with ‘# Database administrative login by Unix domain socket
3. directly below that you will find ‘local all all peer’ change it to ‘local all all trust’
4. save and close the file
5. restart the PostgreSQL server (sudo service postgresql restart)