RSSAmplifier

Blog

Kobrakai

Written by Benjamin Milde

kobrakai.deRSS feed ↗21 posts

Latest posts

ElixirKit ↔ OIDC

At work we had been using elixirkit for a small menu bar app some time last year. Copying livebook’s usage of elixirkit at the time brought us quite quickly to a working proof of concept for the local network proxy we needed. Recently we were looking into adding some authentication to the app to move it closer to being ready for real usage beyond just being a PoC – and given we’ve been exploring…

Data to Responses

The introduction of HEEx in Phoenix 1.7 and the shift toward rendering HTML with function components brought significant changes to how data is transformed into HTTP responses in Phoenix. However, most of these changes were at the surface level. Under the hood, many mechanisms remained unchanged and are still largely backwards compatible. In this blog post, I’ll explore some of Phoenix’s…

Matrjoschka of phoenix communication

Phoenix, the powerful Elixir web framework, is renowned for its real-time capabilities. But with terms like Channels, Presence, and PubSub floating around, it’s easy for newcomers to feel overwhelmed. What exactly do these components do, and how do they relate to each other? If you’ve found yourself puzzling over the differences between Phoenix Channels and PubSub, or wondering when to use which…

Metaprogramming Pitfalls

One of my most liked posts on the elixir forum has long been a post , where I shed some light onto what quote/2 and unquote/1 are about in metaprogramming on elixir. The post seems to have stood the test of time given I still get consistent likes on this post from time to time even with this being over 4 years old at the time of writing this blog post. In this blogpost I want to extend what I’ve…

Bare Websockets

While phoenix used websocket connections for a long time for its Phoenix.Channel abstraction there wasn’t really a good way to use websocket directly up until phoenixs latest major release (1.7). Before that release working with bare websocket connections meant directly calling into the underlying webserver beneight phoenix – :cowboy or more recently Bandit . Before 1.7 the implementation for…

One-to-Many LiveView Form

With Phoenix LiveView becoming more and more popular people try to build more dynamic forms than before. Often this involves forms, which allow editing a parent schema and a one-to-many relationship, where inputs are dynamically added and removed. For example consider a invoice schema, which can have many rows added. There are however a few foot-guns when approaching forms like that. Some due to…

Serve the webfinger protocol with phoenix

With Elon Musk taking over Twitter many, especially tech savvy, people have been looking at Mastodon as an alternative – and so did I. Mastodon is a federated platform, where you join as a user of a certain instance, like for example hachyderm.io , the instance I joined a few days ago. So people can now find me under the account name @lostkobrakai@hachyderm.io . So far so good, but it kinda rubed…

Renderless function components

The introduction of HEEx and function components to Phoenix LiveView brought a lot of improvements for building and maintaining UIs in html to the Phoenix community. A simple example of a function component would be extracting a common chunk of markup to be reused. defmodule Components do use Phoenix.Component def alert ( assigns ) do ~H""" < div class = "alert" > < h3 class = "alert__title" > <%=…

Phoenix Views for JSON APIs

When doing JSON APIs in phoenix one will eventually hit the fact that by default structs cannot be encoded to json by Jason , the default json library of phoenix. Looking at the README of Jason this is quickly resolved by doing something like this: defmodule User do @derive Jason.Encoder defstruct [ :id , :name , :title , :coordinates ] end This does work, but it also has quite a drawback:…

Data fetching using LiveComponents

A common occurance in my phoenix applications is the need to render a list of some items – say companies in a job board application. Let’s start out simple by using the phoenix generators to build up the first bunch of boilerplate for those companies: $ mix phx.gen.live JobBoard Company companies name:string website:string This does generate a liveview module MyAppWeb.CompanyLive.Index , which for…

Unnest for runtime sorted results

The recent ecto 3.5 update introduced the parameterized type Ecto.Enum , which is a great way to use atoms in elixir for signifying things like statuses or types of data. In the database those values are stored as plain strings, so they’re easy to manage there as well – until it comes to sorting by those columns. The simplest solution for sorting not alphabetically, but by e.g. the order the…

LiveView: Double Mount

When using phoenix_live_view people are often worried about their LiveViews being mounted twice for fresh requests. I regularly see people asking for ways around this fact or even why this is needed in the first place. What causes the double mount? A fresh request by an user to a website is always a plain http request. It hit’s MyAppWeb.Endpoint and is served by the plug based pipeline setup on…

Child Specs in Elixir

Child specs are often confusing for people trying to convert the old Supervisor.Spec based syntax to the newer child spec based syntax, people trying to integrate with erlang libraries or because they added more parameters to their start_link functions and now wonder why it fails when trying to supervise the process. Child spec map To start, it’s important to know what a child spec is. It’s a map…

Ecto: When to cast

People starting out with ecto are often confused by all the methods of getting changes applied to a changeset and therefore the database. Especially the options around assocications are often problematic. To bring some clarity into the matter one first needs to understand a thing about the architecture ecto and changesets operate under. Looking at Ecto.Type There are three conceptional types of…

A case against many_to_many

On the Slack Channel for Elixir there’s often the situation of people coming from other languages/frameworks to elixir and especially ecto to ask about “How to model many-to-many relationships”. Most of the times the usual answer comes as a surprise to people, as usage of Ecto.Schema.many_to_many is almost actively discouraged. I’ll try to bring some insights into why this is the case and what’s…

Are elixir deployments really that hard?

I've thought that @rails deployment on a non-Docker or non-Heroku production server was tricky. But that is nothing compared to @elixirphoenix . A million buggy how-tos and deployment tools. Not a single one works. Not a single README works. Always something "small" is missing. &mdash; Stefan Wintermeyer (@wintermeyer) March 17, 2020 At the end of a two day hunt for a https://t.co/nY1mnbHhzA like…

Alternative parameters for phoenix controller actions

You probably know the default parameters of a phoenix controller action callback: def index(conn, _params), do: […] or def create(conn, %{"entity" => entity_params}), do: […] . This seems like a nice default when starting out working with phoenix. All you need to handle a request is the connection and its params after all, right? With me writing this post I obviously came to a different…

Ecto abstract tables

Phoenix 1.3 introduced the concept of contexts in a application. Even though it’s a simple idea on paper — structure you application in modules and functions, which belong to a related context—it’s been an eye-opener for my own projects. I think much more about how high or low level some part of my application is and where the dependencies are between those parts. One recent result of this…

Entity status history using ecto

For a project of mine I recently needed to keep the history of state changes for an entity in my database. Now there are lots of resources out there about event sourcing and all those fancy event driven tools or designs. But I already have a working system and I really only needed status field changes to be persisted (at least for now). The entity table houses the latest status value, so a simple…

Customize Phoenix.View EEx rendering

Getting straightforward HTML templates rendered in Phoenix is really easy. Create a plain View module (see here if you don’t know how), add the related .eex templates, and it just works. What if you need slightly more customization to render your templates? Maybe you need more than a single “layout” template or you want to render a different partial depending on values in the assigns map. In such…

Living Styleguide mit Fractal und ProcessWire

Bei der Thematik von Styleguides und Pattern-Bibliotheken sind sich heutzutage wohl die meisten Entwickler darin einig, dass sie ein wertvolles Tool sind, um ein Projekt auch von Seiten des Interfaces modular zu gestalten. Trotz meines Interesse an der Thematik hat sich jedoch bisher das Anlegen solch einer Bibliothek nie voll in meinen Workflow integriert. Die Hauptschuld dafür hat — zumindest…