What Elixir related stuff are you doing?

Got a late start, but I’ve been doing the Advent of Code (https://adventofcode.com/2020) in Elixir! The puzzles are a decent little challenge, and to add a bit of realism, Part Two isn’t revealed until you submit your answer for Part One… and often has enough in common with Part One that you don’t want to start from scratch, but may need to do significant refactoring, or at least non-trivial extraction of the commonalities.

4 Likes

learning websockets without phx

2 Likes

Writing my master’s thesis, where I implemented a Phoenix API, connecting geospatial data and a Vue.js frontend.

3 Likes

What kind of information are you looking for? I’m looking to adopt LiveView soon.

1 Like

Hey! Several basic things like authenthication, the transition between classic Phoenix templates and LiveView, the routing and redirects between these. Also regarding questions like ,When to use LiveView"?

Do you have a special project you want to migrate?

1 Like

No project I want to migrate. It seems painful to leave the well trodden frontend world so far - all for my company’s work. Heh, trying to push a full stack Elixir microservice (both FE and BE).

When you find your answers, it’d be great to have your solutions shared. :slight_smile:

Though from an outsider’s perspective, routing between LiveView and really HTML views should simply rely on anchor links - they’re all HTML pages at its simplest. But if you mean maintaining state between routing, I don’t know either.

When to use LiveView? I’d probably consider using LiveView for any pages that require user input to work: forms, interactions with lists and charts, etc.

1 Like

Yeah I had problems to maintain the state between LiveView and clasic Phoenix templates since you use a socket in LiveView. I’m doing some babysteps to get a better understanding of these connections.

The question when to use LiveView is for me as Phoenix beginner harder than I thought. At first I used LiveView everywhere but my code became a mess and I had problems with the authenthication in LiveView because it was impossible to use the logged in user on my socket connection. LiveView is perfect for dynamic changes on your page. So you’re right about the interactions on the page. Forms can be in classic Phoenix but some things that really confused me was something like: Should my navigation panel be a LiveView? It’s somethings hard to determine the side effects when you use a LiveView instead of a clasic Phoenix template.

1 Like

Can you be more specific ? I’ve been rewriting a large app made with NodeJS + VueJS and didn’t ran into much issues with that, it was actually easy to get it going (and i’m using LiveView almost everywhere, replacing the vast majority of what my Vue components where doing, but that depend on your needs for sure).

1 Like

Hey! Yes of course :slight_smile:

So my initial problem was to get the currently logged in user inside LiveView. I use Pow for the authentication stuff and the Pow templates are classic Phoenix templates. Since my project was 100% LiveView I hadn’t any controllers. Therefore I couldn’t retrieve the user from the current conn object. The function I had to use was:

Pow.Plug.current_user(conn)

So I rewrote a major part of my project in order to use the classic Phoenix MVC pattern with Phoenix templates and only use LiveView where I have to. Basically I call live_render inside the Phoenix template whenever I have to render a LiveView.

1 Like

Ah yeah ! I’ve read when i started about Pow issues with LiveView. I don’t think it’s been fixed yet (not fully anyway, but there’s an issue thread on the Github repo explaining how to make it work i believe).

Personally, i started by implementing my own system, based on Chris (McCord) “https://pragprog.com/titles/phoenix14/programming-phoenix-1-4/” book. What quite easy to make it work with LiveView (there’s a chapter about LiveView in the book, can’t remember if it talks about authentication…).

Following that, i started using the phx.gen.auth (https://github.com/aaronrenner/phx_gen_auth) project, which if i’m not mistaken, will be included in the next phoenix release (1.6). That’s a generator that adds everything you need for it to work (with every file being well documented so you can adapt to your need).

With that said, if you need some of the bells and whistle of Pow, you might be stuck for the moment. Though i’ve implemented my own permissions / role-based system on top of phx.gen.auth without issues, it was really simple (and well adapted to both my app and my mindset, something i’m rather picky about). So you might want to take a look at doing some of those things yourself, it’s usually not difficult, and i’ve learned a lot about it (Elixir, Phoenix, LiveView…) doing it this way.

To be noted that @mikeclark (and his wife) have a LiveView course who just came out and has a part (over there -> https://pragmaticstudio.com/phoenix-liveview) explaining how to handle authentication with LiveView. I haven’t yet bought that one, but i’ve got some of their other courses, always great, cannot recommend enough :wink:

2 Likes

Here’s what I use:

  def get_user(socket, %{"token_auth" => signed_token}, config) do
    conn = struct!(Plug.Conn, secret_key_base: socket.endpoint.config(:secret_key_base))
    salt = Atom.to_string(Pow.Plug.Session)

    with {:ok, token} <- Pow.Plug.verify_token(conn, salt, signed_token, config),
         {user, _metadata} <- CredentialsCache.get([backend: @pow_store_backend], token) do
      user
    else
      _any -> nil
    end
  end

  def get_user(_, _, _), do: nil

I call this from a mount_defaults function that is called in all the LiveViews’s mount callback.

  def mount_defaults(socket, session) do
    socket
    |> assign(:current_user, Credentials.get_user(socket, session))
  end
2 Likes

Hey yeah I know these resources. I’ve bought every pragmatic programmer Elixir book + the ebooks and I read almost everyday in them. Especially Programming Ecto and Phoenix :smiley:

I really liked how well tested and the functionality of Pow so I tried to use it. I’m also using a role based system. With the gained knowledge from the last months, I would definitely change a few things but this is always the case :smiley:

I really enjoy @mikeclark and his wifes work. I bought the Elixir course bundle last november and they were awesome. I’m currently working through the LiveView Pro Course and I’m at the testing path. Then I’ll throw an eye on their authenthication :slight_smile:

2 Likes

Oh this solution is much more elegant than what I have found so far. I saved the post and test it in my next LiveView project. Thanks a lot! :slight_smile:

2 Likes

Currently working in a methodology to run from the same Phoenix project several sites on the same port 80 and/or 443, like so:

You will build a Phoenix 360 web apps project, that will consist of three websites:

  • app.local - will be the main web app and the only one that runs a web server.
  • links.local - the standalone website that will also be available at app.local/_links.
  • notes.local - the standalone website that will also be available at app.local/_notes.

The web server for app.local will also serve the requests for links.local and notes.local, and will dispatch(not redirect or forward) any request to app.local/_links and app.local/_notes into the same application that runs links.local and notes.local respectively.

1 Like

I’m new to elixir, but I’m hoping that I could make a good JSON API for my messaging platform. Hopefully everything works out :stuck_out_tongue:

1 Like

I had something like that. It requires custom Cowboy handler. PM me and I will try to share it with you.

2 Likes

Now reading the book Programming Phoenix 1.4+. Next book: Real-time phoenix
Here Python / Odoo developer.

1 Like

I’ve been working my way through Programming Elixir and now I’m figuring out Phoenix. I program as a hobby so I’m also discovering how things like Git work.

5 Likes

Update: not much lately, but at least now I have some more solid plans! :slight_smile:

I’m now officially semi-retired. Part of that means I’ll finally have time to read the books I won last year, mostly on Elixir (thanks again, ElixirForum and DevTalk Forum!), and work on a couple Phoenix apps, including possibly porting some of my old Rails apps, some of which are in desperate need of updating anyway. As far as work goes, I’ll be focusing on giving advice rather than “slinging code”… but I’ll be much easier to convince to make an exception if it’s in Elixir.

4 Likes

Still working on https://siteguardian.dev and turning the core of it into a micro-saas template, plus writing a few elixir-related articles and tutorials.

4 Likes