- Notifications
You must be signed in to change notification settings - Fork17
An exploration into a stand-alone library for Plug applications to easily adopt WebSockets.
License
slogsdon/plug-web-socket
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
An exploration into a stand-alone library forPlug applications to easily adopt WebSockets.
Run these:
$ git clone https://github.com/slogsdon/plug-web-socket$ cd plug-web-socket$ mix deps.get$ iex -S mix run run_examples.exs
Go here:http://localhost:4000.
You will be presented with a list of possibleexamples/tests that use a WebSocket connection.
If you're looking to try this in your own testapplication, 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 arun/1
functiongenerated for your router that starts a HTTP/WSlistener. Not sure if this will stay or getreduced to helper functions that aid in thecreation of a similar function. Most likely thelatter will win out to help compose functionality.The big part that it plays is the building of adispatch table to pass as an option to Cowboy thathas an entry for each of your socket routes and acatch all for HTTP requests.
From the topic example:
defmoduleMyApp.TopicControllerdodefhandle(:init,state)do{:ok,state}enddefhandle(:terminate,_state)do:okenddefhandle("topic:"<>letter,state,data)dopayload=%{awesome:"blah#{letter}",orig:data}{:reply,{:text,payload},state}endend
Currently, the function name needs to be uniqueacross all controllers/handlers as its used forthe Events layer.
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 inprogress.
WebSocket is released under the MIT License.
SeeLICENSE for details.
About
An exploration into a stand-alone library for Plug applications to easily adopt WebSockets.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.