WebSocket

Phoenix liveView 0.9.1 breaking changes

If you updated your mix dependencies to use {:phoenix_live_view, “~> 0.9.0”} you might be surprised that …the web socket connection is gone. It’s due to backwards incompatible changes: “LiveView no longer uses the default app layout” which basically means you your liveView redendered template is no longer wrapped by the default layout which (in most cases) includes app.js that makes a connection to live web socket. what’s the fix? The fix is very simple.

Continue reading

age_guard - hex package for age verification

After having fun with building Claritas, I knew I want to publish more packages. Claritas is very simple and doing just one job (darkens or lightern colors) but looks like 79 people (at the time of writing it) were interested to download it. It’s 78 more than I expected :) (that makes me happy! :)) what is age_guard? AgeGuard verifies if a person born at a given date meets provided age requirements.

Continue reading

Claritas - my first hex package

Yesterday I published Claritas - my first hex package! The idea for the package came when I worked on some task and needed dynamically generate a heatmap where the more intensive colour, the higher value of given scale in the heatmap. I used CSS for that, but I though that a pure solution in Elixir would make the code much cleaner. Here it is: Installation The package can be installed by adding claritas to your list of dependencies in mix.

Continue reading

Very good books I have read in 2019

Top books I’ve read this year: Disrupted: My Misadventure in the Start-Up Bubble by Dan Lyons This one is super funny and you once you start it, you can’t stop it. A Newsweek journalist after being laid off (in his early 50s), gets a job in the marketing department of a start-up. Being the oldest (but also the most experienced and …sane) he’s trying to find himself in the world of buzzwords where experience and good ideas lose with incopetence and bumptiousness.

Continue reading

Protecting elixir apps with geoip

This is a common case these days to provide your service only to specific geo locations. It’s easy to bypass it but the law is not following changes in the tech world. Anyway, let’s try to implement a service that is checking client’s IP and allows to get in if the IP is on our whitelist. I’ve chosen ipdata.co (https://ipdata.co/) as staring an integration is very simple and you can register for free.

Continue reading

Elixir plug for securing access to api

There’s many ways to protect access to your API, but this one is very quick to implement. The idea is to block access to a host(s), unless you send a specific header. Let’s name this header as “x-let-me-in” The whole logic can be handled by one function: _ -> Logger.info( "A request from " <> to_string(:inet_parse.ntoa(conn.remote_ip)) <> " has no x-let-me-in header" ) conn |> redirect(to: "/") |> halt() end else conn end end

Continue reading

A practical example of Elixir behaviours

Assuming you know the theory, this example will show how different implementations of Behaviour will be called depending on an environment you’re on. mix new demobehaviour --sup Create a new file formatter.ex in lib/demobehaviour directory: # lib/demobehaviour/formatter.ex defmodule Demobehaviour.Formatter do @callback format(String.t()) :: {:ok, term} | {:error, String.t()} end # lib/demobehaviour/default_formatter.ex defmodule Demobehaviour.DefaultFormatter do @behaviour Demobehaviour.Formatter @impl true def format(str) do {:ok, "default format: " <> str} end end # lib/demobehaviour/custom_formatter.

Continue reading

Basic but useful example of Elixir Protocols

Assuming you know the theory, in this example we’ll provide one login interface for different type of users. mix new protdemo --sup Structs This example shows that passing different data types to our login function will trigger a different implementation. Create a new admin directory under lib/protdemo and put there a new file admin.ex: # lib/protdemo/admin/admin.ex defmodule Protdemo.Admin do defstruct [:admin_name, :password] def login(who), do: {:ok, who.admin_name <> " logged in as Admin"} end # lib/protdemo/user/user.

Continue reading

Datatabase Multitenancy for Elixir applications

If you have never needed splitting data between different schemas (in Postgres), you work on just one (the default) schema - “Public”. Why would you want to use more schemas? For instnace financial institutions may require to keep their data isolated, and multi-tenant schemas is one way doing it. Different schemas may require different ACL, different backup procedures, etc. It’s still possible to do reference between tables in different schemas. I’m using this approach in my current approach but as it’s not released yet, I cannot speak about performance.

Continue reading

Running Elixir Observer on wsl2

I had a occasion to test WSL2. I must say it’s working better than I expected. It’s actually a serious reason people should give up OSX and switch to Windows if they want to play games and still have access to Linux like environment. Docker isn’t supported that great but that’s another story. If you’ve tried to run :observer.start from IEx in WSL2, you’ve probably encountered this error: In WSL2 console, set this export: export DISPLAY=localhost:0 This will tell X11 programs where to display (:0 is the first local display that was started).

Continue reading