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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement