Papillon6814

Papillon6814

How do you return PID in json format?

Hello.
I want to return PID as a response of a request.
But json format does not support PID…

protocol Jason.Encoder not implemented for #PID<0.691.0> of type PID, Jason.Encoder protocol must always be explicitly implemented. This protocol is implemented for the following type(s): Ecto.Association.NotLoaded, Ecto.Schema.Metadata, Any, Atom, BitString, Date, DateTime, Decimal, Float, Integer, Jason.Fragment, List, Map, NaiveDateTime, Time

How do you return it or convert PID to something available in json?

Marked As Solved

dimitarvp

dimitarvp

I am a bit rusty on OTP lately but you can just make sure the process gets automatically killed 5 minutes in the future if a certain message is not received. That message can indeed be a heartbeat which delays the future death of the process 5 minutes after it is received. You should have a happy path in your code where the app gracefully informs the Phoenix API that the user is disconnecting – and that should kill the process immediately. And let the timeout kill the process if the app doesn’t poke the API in time. Something like that.

Also Liked

cmkarlsson

cmkarlsson

The classic way of doing this is to use a Pid registry. Elixir comes with the build-in module Registry which can be used to do just this,

You register the process in the registry with a unique identifier for your user. Then it is easy to lookup and if the process is restarted (perhaps being part of the OTP supervision tree) then it can re-register itself. In case of using a pid directly you might end up with a stale pid.

Aetherus

Aetherus

Why do you want to do that? PIDs are only meaningful in Erlang/Elixir programs.

Aetherus

Aetherus

This only works when the servers and the clients are in the same cluster, but the mobile phones should not be connected to the servers’ epmd because the mobile phones are not trusted, right?

dimitarvp

dimitarvp

The phone shouldn’t be connected to epmd anyway. The phone can send an HTTP request to your Phoenix API which can then return any identifier you fancy – doesn’t have to be the PID itself. You just need a mapping between an ID of some sort (username, email, user’s database ID, whatever) and the PID itself. Registry is one way to do that but honestly, you can just as well keep an Elixir map in a file. :slight_smile:

Basically, you need a mapping between some sort of a user ID and the PID of the process responsible for the user.

Aetherus

Aetherus

I understand. There remains a problem though, how to detect a disconnection from a phone? Anyway, the end-of-life of a process should not be on the hand of a phone because it can disconnect at any time without telling the server anything. A heartbeat policy should be fine.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

We're in Beta

About us Mission Statement