0

I am a bit out of my comfort zone here, so looking for a bit of guidance. I am trying to access an api to display live metrics, using phonic-elixir (https://www.npmjs.com/package/phoenix-elixir) - am just sort of trying to get it running first, so have loaded up their example code and connecting to an api (forgive me if the terminology is all wrong, I am new at this!)

This is my code:

import {Socket} from 'phoenix-elixir';

let socket = new Socket('ws://API_URL_HERE', {params: {'auth-token': 'AUTH_TOKEN'}})

socket.connect()

let channel = socket.channel('updates:new', {})
channel.join()
  .receive('ok', resp => { console.log('Joined successfully', resp) })
  .receive('error', resp => { console.log('Unable to join', resp) })

channel.on('update', payload => {
  console.log('Received: ' + payload);
  console.log(payload);
})

export default socket

When I run babel index.js | node I am getting the error: this.transport = opts.transport || window.WebSocket || LongPoll; and ReferenceError: window is not defined

Just some advice to point me in the right direction would be fantastic. Is window not defined because it needs a dom? Do I need a server to run this in?

Thank you :)

Siguza
  • 21,155
  • 6
  • 52
  • 89
Le Moi
  • 975
  • 2
  • 15
  • 41
  • Probably relevant: https://stackoverflow.com/q/10984629 – Siguza Mar 17 '16 at 19:45
  • New to working with this API? javascript? nodejs? Or all of the above? Sounds like all of the above and this is more on the advanced side. My advice is to read up on javascript basics so you understand your own question. If you cannot ask the question then you'll never understand the answer. – Just Aguy Mar 17 '16 at 19:56

2 Answers2

1

I just ported the client to be compatible with node.JS.

Here is the link https://github.com/mcampa/phoenix-channels

The difference with the original client is that this does not use long-polling and you need to pass the absolute url instead of the relative url.

To install it run:

npm install --save phoenix-channels

Same API as the original:

const { Socket } = require('phoenix-channels')

let socket = new Socket("ws://example.com/socket")

socket.connect()

// Now that you are connected, you can join channels with a topic:
let channel = socket.channel("room:lobby", {})
channel.join()
  .receive("ok", resp => { console.log("Joined successfully", resp) })
  .receive("error", resp => { console.log("Unable to join", resp) })
Mario Campa
  • 4,092
  • 1
  • 26
  • 25
0

phoenix-elixir is client-side library that is supposed to be used in browsers not in node environment. You should create html page with your code and open it in browser to test it out.

Stubb0rn
  • 1,269
  • 12
  • 17