Today I Learned

hashrocket A Hashrocket project

3 parts of routing a websocket

First, there's a socket declaration in the lib/appname/endpoint.ex file.

  socket "/socket", AppName.UserSocket

Second, there's a transport declaration in the web/channels/user_socket.ex file.

  transport :ws, Phoenix.Transports.WebSocket

Now, the websocket will be available at the url ws://localhost:4000/socket/ws the final path segment of the url being added by the first argument of the transport declaration.

Finally, the channel topic is also declared in the user_socket.ex file.

channel "app_topic:*", AppName.MyChannel

Now every json body that has a topic attribute of app_topic will be routed to MyChannel.AppName where it will be handled appropriately based on the event attribute.

{"topic":"app_topic:something","event":"phx_join","payload":{},"ref":"1"}
See More #elixir TILs
Looking for help? At Hashrocket, we 💜 Elixir! From our many Elixir client projects, to sponsoring the Chicago Elixir Meetup, to the source code for this application, we are invested in this community. Contact us today to talk about your Elixir project.