Today I Learned
How to Get State of Any GenServer in Elixir?
To get state of any process in erlang/elixir use :sys.get_state/1
By name:
:sys.get_state(MyGenServer)
By pid:
:sys.get_state(pid)
How to Make SSH Service Discoverable in Elixir Using mDNS?
To make service discoverable we need following services registered:
[
# create domain for an ip
%Mdns.Server.Service{domain: "somedomain.local", data: :ip, ttl: 450, type: :a},
# make service discoverable
%Mdns.Server.Service{domain: "_services._dns-sd._udp.local",data: "_ssh._tcp.local",ttl: 4500, type: :ptr},
# register ssh service
%Mdns.Server.Service{domain: "_ssh._tcp.local",data: "SOME NAME._ssh._tcp.local",ttl: 4500, type: :ptr},
# point service to our domain and port (22)
%Mdns.Server.Service{domain: "SOME NAME._ssh._tcp.local",data: {0,0,22, 'somedomain.local'},ttl: 4500,type: :srv},
# empty txt service (some tools expext that)
%Mdns.Server.Service{domain: "SOME NAME._ssh._tcp.local",data: [],ttl: 4500,type: :txt})
] |> Enum.each(&Mdns.Server.add_service/1)
Free PubSub in Elixir Phoenix Application
If you need a pubsub, to connect LiveViews for example just use YourAppWeb.Endpoint.
YourAppWeb.Endpoint.subscribe("topic")
YourAppWeb.Endpoint.broadcast("topic", "event", %{data: "data"})
def handle_info(%{event: "event", topic: "topic", payload: payload}) do
# Do whatever you want
end
How to Quick Escape SSH Session?
Just type: [enter]~.
Yes, Enter, tilde and dot!