ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - slogsdon/plug-web-socket: An exploration into a stand-alone library for Plug applications to easily adopt WebSockets.
An exploration into a stand-alone library for Plug applications to easily adopt WebSockets. - slogsdon/plug-web-socket
Visit Site

GitHub - slogsdon/plug-web-socket: An exploration into a stand-alone library for Plug applications to easily adopt WebSockets.

GitHub - slogsdon/plug-web-socket: An exploration into a stand-alone library for Plug applications to easily adopt WebSockets.

WebSocket

Build Status Coverage Status Hex.pm Version

An exploration into a stand-alone library for Plug applications to easily adopt WebSockets.

Viewing the examples

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 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:

defmodule MyApp.Router do
  use Plug.Router
  use WebSocket

  # WebSocket routes
  #      route     controller/handler     function & name
  socket "/topic", MyApp.TopicController, :handle
  socket "/echo",  MyApp.EchoController,  :echo

  # Rest of your router's plugs and routes
  # ...

  def run(opts \\ []) do
    dispatch = dispatch_table(opts)
    Plug.Adapters.Cowboy.http __MODULE__, opts, [dispatch: dispatch]
  end
end

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.

Add the necessary bits to a module

From the topic example:

defmodule MyApp.TopicController do
  def handle(:init, state) do
    {:ok, state}
  end
  def handle(:terminate, _state) do
    :ok
  end
  def handle("topic:" <> letter, state, data) do
    payload = %{awesome: "blah #{letter}",
                orig: data}
    {:reply, {:text, payload}, state}
  end
end

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 message
topic = "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.

License

WebSocket is released under the MIT License.

See LICENSE for details.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory