You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You will be presented with a list of possible
examples/tests that use a WebSocket connection.
Integrating with Plug
If you're looking to try this in your own test
application, do something like this:
defmoduleMyApp.RouterdousePlug.RouteruseWebSocket# WebSocket routes# route controller/handler function & namesocket"/topic",MyApp.TopicController,:handlesocket"/echo",MyApp.EchoController,:echo# Rest of your router's plugs and routes# ...defrun(opts\\[])dodispatch=dispatch_table(opts)Plug.Adapters.Cowboy.http__MODULE__,opts,[dispatch: dispatch]endend
For the time being, there is a run/1 function
generated for your router that starts a HTTP/WS
listener. Not sure if this will stay or get
reduced to helper functions that aid in the
creation of a similar function. Most likely the
latter will win out to help compose functionality.
The big part that it plays is the building of a
dispatch table to pass as an option to Cowboy that
has an entry for each of your socket routes and a
catch all for HTTP requests.
Currently, the function name needs to be unique
across all controllers/handlers as its used for
the Events layer.
Broadcast from elsewhere
Need to send data out from elsewhere in your app?
# Build your messagetopic="my_event"data=%{foo: "awesome"}mes=WebSocket.Message.build(topic,data)json=Poison.encode!(mes)# Pick your destination (from your routes)name=:handle# Send away!WebSockets.broadcast!(name,json)
This needs to be nicer, but this is still in
progress.