Comments (9)
I redesigned the way how Drab injects its code to your App. Now, it is done in the MyApp.UserSocket
instead of MyApp.Endpoint
:
defmodule MyApp.UserSocket do
use Phoenix.Socket
use Drab.Socket
channel "mychannel:*", MyApp.Channel
# and so on...
end
Please notice that by default Drab tries to use default "/socket" path, in case you want to use a different one, you need to specify it in the config:
config :drab,
socket: "/drab/socket"
On the client side, there is a global object Drab
and you can use its socket: Drab.socket
.
This change is included in b3b2d8c, and will be a part of release v0.3.0
, coming soon.
from drab.
JS side is simple, there is a global object Drab
, I will add socket as a property.
var channel = Drab.socket.channel("mychannel:whatever", {})
channel.join()
What concerns me is how to define additional channels in your application. Now you run the socket behind the scene, by using Drab.Endpoint
. We need to define an API to tell Drab to create additional channels. It might look like:
use Drab.Endpoint, add_channels: [{"mychannel:whatever", DrabPoc.Socket}]
where DrabPoc.Socket
is a module in your Phoenix application.
Or even like below, to be more consistent with Phoenix:
use Drab.Endpoint
drab Drab.AddChannel, "mychannel:whatever", DrabPoc.Socket
from drab.
I'd just have the user add a single "drab:*" channel, then you can internally dispatch inside of it as it will get all channel requests that start with "drab:", nicely compartmentalized. :-)
from drab.
Awesome! This also means I can use my custom token authentication that I use on my main socket. :-)
from drab.
Which reminds me, I either need to create the socket on my side, or I need to set up yours a special way before you connect and set up certain parameters, where is that documented? :-)
from drab.
Yes, now you need to create a socket on your side (notice it is done by default in endpoint.ex and user_socket.ex).
Whenever you want to use a path different than "/socket", you need to configure this.
Updated documentation is coming with v0.3.0 in a couple of days ;)
from drab.
I meant on the javascript side. :-)
Specifically I need to pass a set of custom parameters and set some headers first.
Or I can hand your library an already initialized socket somehow. ^.^
from drab.
Drab always starts on the browser side on page load, it connects and it is passing some Token with internal information (from which controller and action is was rendered).
This is why you probably can't use your own authentication, if it rely on connect
callback in the UserSocket
. Your connect
callback will never be launched, because Drab intercepts it first. (It is confusing and must be changed or very well documented before releasing v0.3.0)
However, there are 2 ways to accomplish the authentication:
- first at all, Drab Commander can have an access to the Plug Session, so if you store something like "userid" there, you are saved
- additionally, you can pass your own parameters to the Drab.Client when injecting Drab JS:
<%= Drab.Client.js(@conn, user_id: 4, name: "Zdzisław") %>
this will add the additional fields to the Drab Token, and after connect they will just appear in assigns:
%Phoenix.Socket{assigns: %{
__action: :index, __controller: DrabPoc.PageController,
__drab_pid: #PID<0.1430.0>, __url_path: "/drab",
name: "Zdzisław", user_id: 4}, ...}
from drab.
Changes released in https://github.com/grych/drab/releases/tag/v0.3.0
from drab.
Related Issues (20)
- Drab resets scroll position when updating DOM HOT 1
- Unable to join the Drab Channel {reason: "join crashed"} HOT 2
- Does `broadcast_poke` pass "origin" socket's assigns to all browers? HOT 4
- sender always return true for checkbox
- function Phoenix.HTML.Engine.fetch_assign/2 is undefined or private HOT 17
- (ArgumentError) assign @xxx not available in eex template. HOT 7
- Compact log message HOT 13
- Important information about Tomasz (I am his colleague) HOT 4
- ReferenceError: event is not defined in FireFox HOT 9
- Cohabitation with LiveView
- Drab is unable to find the endpoint of ':cool_interface'. HOT 6
- this.Socket is not a constructor HOT 3
- Support for hot code upgrade in production HOT 1
- Browser Support HOT 6
- exec_js return type unmatched when using dialyzer HOT 2
- partials do not work HOT 6
- mismatch template HOT 7
- drab-click in nested templates HOT 10
- Support Phoenix 1.5 HOT 3
- Is it possible to use drab without elixir? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drab.