CAIOHOBORGHI

CAIOHOBORGHI

Can't connect WebSockex client to Socket.IO Server

Hello guys, how are you?

I’m trying to connect to a local socket.io server using WebSockex but when the application tries to run

WebSockex.start_link("ws://socket:8007", __MODULE__, state)

it always throws the error

** (EXIT) %WebSockex.ConnError{original: :closed}

The problem is that the server isn’t closed… I used docker-compose to run a server and a client container and with node it works.

Thats my server.js file:

const http = require("http").createServer();
const io = require("socket.io")(http);
const port = 8007;

var connectedClients = [];

http.listen(port, "0.0.0.0", async () => {
  console.log("server listening on port:" + port);
});

io.on("connection", async (socket) => {
  connectedClients.push(socket.id);
  socket.on("message", async (body) => {
    socket.broadcast
      .to(socket.id)
      .emit("message", "Message sent successfully!");

    var command = JSON.parse(body);
    if (command.action == "add_alert") {
      io.emit("add_alert", command);
    } else {
      io.emit("add_alert", "error receiving alert!");
    }
  });
});

Thats my client.js file

const io = require("socket.io-client");

const socket = io.connect("ws://socket:8007");
socket.on("connect", () => {
  var obj =
    '{"action":"add_alert","data":{"user":{"id":1231298301283,"name":"Caio Borghi","phone":"+55900000000","alerts":[{"id":"hashID1","code":"ELET6","field":"Close","comparison":"<=","value":11.9,"message":"ELET6 Close price is lower than 11.9!"},{"id":"hashID2","code":"BBDC4","field":"Close","comparison":"<=","value":15.9,"message":"BBDC4 Close price is lower than 15.9!"}]}}}';
  socket.emit("message", obj);
  console.log("Client is Listening the Local connection.");
});

socket.on("add_alert", async (body) => {
  console.log("Alert received: " + JSON.stringify(body));
});

And in my docker-compose i`m connecting all 3 containers in the same network…

The big deal here is that the client container works perfectly pointing to the same URL that my Elixir application (ws://socket:8007) but when I try to connect through Elixir, it never works.

Do you know if there`s any config I need to change to be able to connect to “local” websockets with WebSockex? Or any tip of what I should try to make it work?

Thanks in advance, any help is welcome!

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement