Giter VIP home page Giter VIP logo

Comments (9)

grych avatar grych commented on September 15, 2024 1

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.

grych avatar grych commented on September 15, 2024

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.

OvermindDL1 avatar OvermindDL1 commented on September 15, 2024

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.

OvermindDL1 avatar OvermindDL1 commented on September 15, 2024

Awesome! This also means I can use my custom token authentication that I use on my main socket. :-)

from drab.

OvermindDL1 avatar OvermindDL1 commented on September 15, 2024

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.

grych avatar grych commented on September 15, 2024

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.

OvermindDL1 avatar OvermindDL1 commented on September 15, 2024

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.

grych avatar grych commented on September 15, 2024

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.

grych avatar grych commented on September 15, 2024

Changes released in https://github.com/grych/drab/releases/tag/v0.3.0

from drab.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.