mrcly
Hi,
i am running a TCP client using gen_tcp inside a GenServer, which is called by DynamicSupervisor using Supervisor.start_client(). Whenever I call this method, the output (which is logged with IO.puts) is blocking the REPL. I start the application via iex -S mix. In the future, it is planned to execute some actions when a specific message is received via TCP, but that’s not implemented currently. However, I need to perform multiple TCP-clients at once and after calling Supervisor.start_client() once, the REPL is blocked and another TCP client cant be spawned.
How can I run this TCP-Client in a background/non-blocking process?
Thanks in advance.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #elixirconf-us
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #security
- #elixirconf-eu
- #api
- #forms
- #performance










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
evadne
Do not do the work in
init/1. Init is supposed to return quickly. Instead use continuation. See GenServer — Elixir v1.20.2. Return{:ok, state, {:continue, continue}}in yourinit/1callback and continue your setup / whatever other sort of blocking operation inhandle_continue/2.entone
Yeah, without seeing your client code, it’s hard to say why it would be blocking. Even a long running
initcallback shouldn’t block indefinitely.My guess would be that your making a blocking call from within your
initcallback.mrcly
Hey, thanks for the answer.
So in general, in my
GenServerimplementation, insideinit/1i call:gen_tcp.connect, a couple of:gen_tcp.sendfor authentication and other stuff, and at the end:gen_tcp.recvwhich calls itself recursively to receive data all the time until the client is stopped, which is possible the reason why its blocking the repl.hauleth
Yes, you should never do any “heavy lifting” stuff in
init/1, and for sure no infinite loops. You in general shouldn’t do any infinite loops ingen_serveras there is already an internal loop for that and you should rely on messages instead.entone
Here’s an example of TCP Client I’ve been using lately, this uses
active: :onceand receives messages inhandle_info, it also uses SSL instead of TCP, but the interfaces are the same, should be able to swap out:sslfor:gen_tcppretty easily.In addition it uses a
Registryto publish the messages once they’ve been properly parsed.One thing to note about receiving your messages, this one can receive multiple messages per
handle_info, so the parser is actually recursive. If your messages are null terminated, or some other termination, you can set that when you connect to the tcp socket. Check out all the options when connecting. gen_tcp — OTP 29.0.2 (kernel 11.0.2)derek-zhou
If everything is done in
init/1, then you don’t need a GenServer. Justspawn_link/3it:, ie:Now you have a supervise-able worker process just like a GenServer, except everything is in
init/1