Licenser
Plan old websockets in phoenix but without magic
Hi, I’m looking for a way to create a good old web socket and to send data from and to the browser. The result is going to end up in xterm.js (browser-based terminal), so the whole channels thing is out of the question (I don’t want to re-write xterm.js just to support channels).
I’ve tried phx_raws, but Phoenix still wants to mess with the messages the process gets and breaks stuff.
I’ve looked at overwriting HTTP: on the config, but that breaks the live reloader.
I’m quite lost here, I just want a good old WebSocket, the logic is super simple: if it receives {:binary, bla} from the socket, it calls a library, if it receives {:data, bla} it sends it to the socket. A bit auth sprinkled on it, and that’s it (literally a copy of this src/wiggle_console_h.erl · test · Project-FiFo / FiFo / wiggle · GitLab).
Any advice, am I missing a flag to turn off ‘magic’?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 10 of 36 Posts
OvermindDL1
Phoenix’s websocket support is via an abstraction of ‘Channels’ and phoenix does not really have any websocket helpers itself.
Rather what you want, if you want raw websockets, is just to use the cowboy API underneath Phoenix directly. Phoenix tends to not rewrite anything into itself if one of it’s dependencies already handles it, and as cowboy handles raw websockets already then it does nothing special with it, just implement cowboy’s API and put cowboy in your supervision tree, hooking in phoenix in the right place too. There are docs, hmm… somewhere on how to do it, maybe someone will link them? Hard for me to google at moment… ^.^;
Licenser
Thanks mate, yes I looked at the cowboy stuff, Document use of "raw" websockets · Issue #234 · phoenixframework/phoenix · GitHub mentions
dispatch_optionwhich seems to no longer exist. The only thing I’ve found is completely hand crafting the dispatch rules which seems to be badly documented and blows up in dev as there are some secret endpoints addedOvermindDL1
Yep that’s it, and yep you have to be sure to add in Phoenix’s stuff back again, can just mostly copy/paste it’s setup code though (and add your own). ^.^;
It would be nice if Phoenix’s endpoint setup had a method to add extra things to the cowboy setup… Maybe PR it?
Licenser
the problem is that approach is not working. Not all required endpoints are not documented and even searching the entire repo doesn’t show where they come from
OvermindDL1
Well what you ‘usually’ put in your application is:
And your Endpoint uses Phoenix’s Endpoint, so checking it’s use function is:
https://github.com/phoenixframework/phoenix/blob/v1.3.0/lib/phoenix/endpoint.ex#L440-L449
And the endpoint module behaviour defines
initand other such interesting things, which delegate to thePhoenix.Endpoint.Supervisor, and here is the interesting part there:https://github.com/phoenixframework/phoenix/blob/v1.3.0/lib/phoenix/endpoint/supervisor.ex#L65-L71
And the interesting part is probably
server_childrenat:https://github.com/phoenixframework/phoenix/blob/v1.3.0/lib/phoenix/endpoint/supervisor.ex#L92-L101
But it becomes hairy, so maybe wait for a Phoenix person like @chrismccord or so? ^.^;
But overall you could get the supervisor data like that then mutate it to add your own part, or maybe Phoenix has a way to add in custom cowboy handlers yet?
chrismccord
The cowboy handler docs allow you to set up a cowboy websocket handler to use as you see fit:
https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/endpoint/cowboy_handler.ex#L8-L52
You may have tried these docs as you said you have issues but it’s not clear what problems you’re encountering. Note the caveats in the docs where these are cow1 specific and changes here not subject to semver.
sasajuric
Have you tried this? This allows you to specify custom Cowboy dispatch list, and then you should be able to handle websocket connections without using the channels protocol.
Licenser
Hi sorry you’re right I was really unclear about the issue. I am a bit confused myself by the whole thing
I did set up a custom dispatcher based on that docs but the moment I did the logs got spammed by errors:
I interpret this as the live reload server endpoint missing, I hoped the :_ case would provide that but it seems it’s not and I couldn’t figure out where in the phoenix code it was defined to add it like the
/socket/websocketendpointsatom99
Would you mind sharing in what way Phoenix interferes and breaks your code? I just saw your issue, did you get to configure it? I’m wondering so I can tackle issues down before working on this.
Going with a manual Cowboy configuration otherwise is as well a good option
Licenser
Sure! This involves a bit of guess work about how phoenix works as it’s really a big black box to me.
The problem, from what I tell, is that Phoenix’s Socket will interpret any message that arrives at the Socket process and tries to handle it.
Simplified what I try to write is a websocket proxy. Data that comes from a process and gets send to the browser. Data that comes from the browser gets send to a process.
The issue start that when the process sends it data to the Socket, the socket treats it the same way it would treat websocket data, meaning it’ll start trying to apply opcodes and things to id.
So when the process sends
socket_pid ! {:data, some_binary}then the socket crashes as Phoenix tries to parse that as something coming from the web socket.