Phoenix: Phoenix js client on node.js or React-Native

Created on 28 May 2017  路  3Comments  路  Source: phoenixframework/phoenix

I was eager to start a new project using phoenix channels communicating to some devices running node.js until I got this error:

ReferenceError: window is not defined
    at new Socket (/Users/mcampa/raspberry/printer-client/node_modules/phoenix/priv/static/phoenix.js:633:40)
    at Object.<anonymous> (/Users/mcampa/bh/raspberri/printer-client/src/main.js:11:14)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:393:7)
    at startup (bootstrap_node.js:150:9)

Then I found out that phoenix client only runs on browsers 馃槩

So I was hoping to know if there is any other library/plan/trick to run phoenix client on node.js
How difficult would be to refactor it to work on node.js?
What about react-native? is that supported?

I would love to see a client written in Elixir that I could use with nerves-project

Most helpful comment

Folks are running phoenix.js on react-native and node.js just fine, but they have to tell it to use a node websocket transport, not the default window.WebSocket, i.e.

var ws = require("websocket")
var Socket = require("phoenix").Socket
var socket = new Socket("/socket", {transport: ws})
...

All 3 comments

Folks are running phoenix.js on react-native and node.js just fine, but they have to tell it to use a node websocket transport, not the default window.WebSocket, i.e.

var ws = require("websocket")
var Socket = require("phoenix").Socket
var socket = new Socket("/socket", {transport: ws})
...

Apologies for reopening, but for anyone who stumbles upon this, the current way to do this would require explicitly using the W3C API version:

let W3CWebSocket = require('websocket').w3cwebsocket

Additionally, I'm using this in Nuxt for SSR, and then letting the Phoenix.js logic decide in the browser. Something like this:

if (isServer) socketOpts.transport = W3CWebSocket

@bjunc, that's exactly what I had to do https://github.com/mcampa/phoenix-channels/blob/master/src/socket.js#L11

I have that repo for running channels on a raspberrypi. Feel free to fork it. It might be outdated though. However, is intended to be used on server side only.

There are also a few other issues where it is referencing window that I had to fix too.

You can install it from npm:

npm i phoenix-channels
Was this page helpful?
0 / 5 - 0 ratings