aschafers

aschafers

Hello,

My question: Is it possible to use the phoenix.js file in other project.
Actualy i want build a websocket server with phoenix an connect it from an external nodeJS app.
But when i require(‘phoenix’)

I get this error:

Cannot read property ‘WebSocket’ of undefined
at new e (/Users/user1/node_modules/phoenix/priv/static/phoenix.js:1:10119)

Showing Posts 1 to 7

kokolegorille

kokolegorille

Hello welcome,

did You try to use the npm package? You can find it here
https://www.npmjs.com/package/phoenix

mhanberg

mhanberg

Expert LSP Core Team

WebSocket is a browser API and isn’t available in Node.

aschafers

aschafers OP

hi, kokolegorille

Yes, I try to use this package.

I’m sure it’s possible.

jdj_dk

jdj_dk

I am using phoenix-channels to connect to our Phoenix app from Node.js.

https://www.npmjs.com/package/phoenix-channels

Works fine so far

aschafers

aschafers OP

Thx jdj_dk

For test i have created a little node script

const { Socket } = require('phoenix-channels')
let socket = new Socket("ws://127.0.0.1:4000/socket")
let channel = socket.channel("interlocutors:fezfezfzef", {})
channel.join()
  .receive("ok", resp => { console.log("Joined successfully", resp) })
  .receive("error", resp => { console.log("Unable to join", resp) })

But when i run it nothing seems to be happening.
My elixir code work beacause when i send raw socket with GitHub - eliihen/wsta: A CLI development tool for WebSocket APIs · GitHub all is good.

defmodule ServerWeb.UserSocket do
  use Phoenix.Socket

  ## Channels
   channel "interlocutors:*", ServerWeb.InterlocutorsChannel
 
  # Socket params are passed from the client and can
  # be used to verify and authenticate a user. After
  # verification, you can put default assigns into
  # the socket that will be set for all channels, ie
  #
  #     {:ok, assign(socket, :user_id, verified_user_id)}
  #
  # To deny connection, return `:error`.
  #
  # See `Phoenix.Token` documentation for examples in
  # performing token verification on connect.
  def connect(_params, socket, _connect_info) do
    {:ok, socket}
  end

  # Socket id's are topics that allow you to identify all sockets for a given user:
  #
  #     def id(socket), do: "user_socket:#{socket.assigns.user_id}"
  #
  # Would allow you to broadcast a "disconnect" event and terminate
  # all active sockets and channels for a given user:
  #
  #     ServerWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
  #
  # Returning `nil` makes this socket anonymous.
  def id(_socket), do: nil
end

defmodule ServerWeb.InterlocutorsChannel do
  use ServerWeb, :channel

  def join("interlocutors:" <> int_id, _params, socket) do
    IO.puts(int_id)

    {:ok, socket}
  end

  def handle_in("new_message", _params, socket) do
    IO.puts("hello")

    {:noreply, socket}
  end

end

I don’t understand why it doesn’t work for me.

aschafers

aschafers OP

Oups sorry i forgot the socket.connect(). it’s work nice thx !!!

raphaklaus

raphaklaus

This package was really helpful. Thanks.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews