code.cycling
code.cycling

Reputation: 1274

Trying to connect to a Phoenix channel, but getting SOCKET is undefined

I'm a newbie in Phoenix and I was trying to learn Channels.

My frontend which is in Angularjs run on port :3000. And my backend which run on port :4000.

I am getting an error of Socket is not defined.

index.html

<script src="/node_modules/phoenix/priv/static/phoenix.js"></script>


<script>
var socket = new Socket("ws://localhost:4000", {
  logger: ((kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }),
  transport: WebSocket
});
socket.connect();</script>

Upvotes: 0

Views: 756

Answers (1)

Jonas Dellinger
Jonas Dellinger

Reputation: 1364

Upon directly including the phoenix.js file via script-Tag, you will need to use the window.Phoenix object which includes all exported phoenix modules

<script>
var socket = new Phoenix.Socket("ws://localhost:4000", {
  logger: ((kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }),
  transport: WebSocket
});
socket.connect();
</script>

Included in window.Phoenix:

  • Ajax
  • LongPoll
  • Presence
  • Socket
  • Channel

Upvotes: 2

Related Questions