I want to offer some free functionality on my site to unauthenticated users and need to limit & protect it from abuse (IP logging being crucial). Doing it via API requests is easy(conn.remote_ip) but can’t find anything similar for sockets.
Is it possible to get IP from socket/channel connections? Any examples?
at this time you can’t. You can’t access the connection in channels because channels are transport agnostic (websocket, longpolling …), the straightforward solution is to pass the IP address as a parameter when connecting to the socket using some sort of ajax request to get the client IP
Since Phoenix 1.4, you can get connection information from the underlying transport. What kind of information you get is transport dependent, but with the WebSocket transport it is possible to retrieve the peer info (ip address) and a list of x- headers (for x-forwarded-for resolving).
Configure your socket like this in your endpoint.ex: