Skip to content

Instantly share code, notes, and snippets.

@cortezcristian
Last active August 12, 2016 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cortezcristian/9a99fcb08c0e1bbf752410c79b649260 to your computer and use it in GitHub Desktop.
Save cortezcristian/9a99fcb08c0e1bbf752410c79b649260 to your computer and use it in GitHub Desktop.
Phoenix and Elixir

My first impressions using Phoenix and Elixir.

Installation

In OS X simply run:

$ brew install elixir

A few months ago a person aproached me in the colission conference to talk about Elixir, I was programming a lot with NodeJS so I thought it wasn't ready for production. Recently I've seen an amazing inspirational demo app called phoenix-trello. It's a trello tribute build on top of Phoenix (also using Webpack, React and Redux), take a look here: https://phoenix-trello.herokuapp.com

After playing a little bit with its REPL (just type iex), found some useful material and also a Vim plugin.

After that I decided it was time to install Phoenix so I follow the official installation guide

$ mix local.hex
$ mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez

I've heard a lot of things about this project, specially performance comparisons with NodeJS and Express.

Creating my first project

So let's create a web project.

$ mix phoenix.new web
$ cd web
$ mix phoenix.server
...
[info] Running Web.Endpoint with Cowboy using http://localhost:4000
[info] GET /

What is this sorcery? After watching the video in http://www.phoenixframework.org/docs I was able to understand a little more about the structure.

Right after that some error appeared because I didn't install PostgreSQL as suggested in this guide elixirphoenix.bash

$ mix phoenix.server
...
[info] Running Web.Endpoint with Cowboy using http://localhost:4000
[info] GET /
[debug] Processing by Web.PageController.index/2
  Parameters: %{}
  Pipelines: [:browser]
[info] Sent 200 in 1ms
[error] Postgrex.Protocol (#PID<0.2935.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused

So installing it:

$ brew install postgresql
...
To have launchd start postgresql at login:
  ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
  postgres -D /usr/local/var/postgres

After that we do start the database, do a psql to get into it and if some error arise just create a new database

$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
$ psql -h localhost
psql (9.5.1)
Type "help" for help.

cristian=#\q

$ createuser -d postgres # new user will be allowed to create databases. See config/dev.exs
$ psql -d postgres
postgres=# createdb
postgres-# \l
$ mix ecto.create
$ mix phoenix.server
[info] Running Web.Endpoint with Cowboy using http://localhost:4000
12 Aug 00:02:00 - info: compiled 6 files into 2 files, copied 3 in 1.5 sec
[info] GET /
[debug] Processing by Web.PageController.index/2
  Parameters: %{}
  Pipelines: [:browser]
[info] Sent 200 in 53ms
npm install -g brunch^C
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
^C%    

And that's it! It looks like I forgot mix ecto.create from the begining.

It's working as expected now! I learned about some nodejs utils like brunch that are convinient if you have them installed:

$ npm install -g brunch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment