mayulu

mayulu

How can I get the tcp socket in a process that started a redix connection?

Hi,

The question I tried a lot and have got no idea.

After

{:ok, conn} = Redix.start_link(host: "127.0.0.1", port: 6379) 

conn is PID like #PID<0.554.0>

Then I send some special command to redis server:

{:ok, OK} = Redix.start_link(conn, [...some special command...]) 

The special redis server may then further push a series responses to conn separately and constantly.

I want to use :

:gen_tcp.recv(socket, 0)

to receive data from redis server in process #PID<0.554.0>

But conn is a PID (etc. TCP Client) not a socket, So how can I get socket from the PID.

Anyone can give me a glue?

Thanks

Marked As Solved

idi527

idi527

:wave:

Maybe there is a way to do it via redix? Either way, redix probably keeps the socket in it’s state, you can try a hack like :sys.get_state(redix_conn) to find out.

:gen_tcp.recv(socket, 0) might not work if the socket is started in active: true mode. Seems like it is started with active: :once. And is reset to that mode after each received packet. So you might try something like

%{socket: socket} = :sys.get_state(redix_conn)
:gen_tcp.recv(socket, 0)

But a more correct approach is most likely possible, since you might receive the data you don’t need, which would in turn make other queued requests fail.

I’d probably fork redix and add a way to subscribe for updates, especially if it’s some special redis server.

Also Liked

mayulu

mayulu

I got the socket after following your instruction.

:sys.get_state(conn)

returned the following:

{:connected, 
    %Redix.Connection{
        backoff_current: nil, 
        client_reply: :on, 
        connected_address: "127.0.0.1:6379", 
        counter: 0, 
        opts: [
                socket_opts: [], 
                ssl: false, 
                sync_connect: false, 
                backoff_initial: 500, 
                backoff_max: 30000, 
                log: [
                    disconnection: :error, 
                    failed_connection: :error,
                    reconnection: :info
                    ], 
                exit_on_disconnection: false, 
                timeout: 5000, 
                host: '127.0.0.1', 
                port: 6379
            ], 
        socket: #Port<0.9>, 
        socket_owner: #PID<0.180.0>, 
        table: #Reference<0.3121165064.4228775937.104953>, 
        transport: :gen_tcp
    }
}

socket is a Port<0.9>
Then after

:gen_tcp.recv(socket, 0)

It get a error, the socket did not work

{:error, :einval}

But you figured me out, it is not the right way. It should be done in the redis lib.

I should fork Redix or find other redis lib.
(similar to redigo lib in golang, there is a Receive function in such situation.)

Thank you very much.

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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement