Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the "receive" function available to caller #46

Open
fpalluel opened this issue Nov 23, 2016 · 6 comments
Open

Make the "receive" function available to caller #46

fpalluel opened this issue Nov 23, 2016 · 6 comments

Comments

@fpalluel
Copy link

Hello,
I just started playing with phoenix's channels and found a small issue with this library.
I try to get information from the reply of the "join" call. According to the doc, this could be done using the receive function :

channel.join()
  .receive("ok", resp => { console.log("Joined successfully", resp) })

But when I try to use ember-phoenix :

const channel = this.joinChannel("room:123", {
  nickname: "Mike"
  })
  .receive("ok", resp => { console.log("Joined successfully", resp) });

this throws a "receive function is not defined"
So I had to make ember-phoenix return the channel.join() object instead of the channel object, and it works. But now, to access the channel object itself, I have to do this :

    const channel = this.joinChannel("room:123", {
      nickname: "Mike"
    });
    // add message handlers
    channel.channel.on("notification", () => _onNotification(...arguments));

Is there a more "elegant" way to address this issue ?
Thanks !

@juanazam
Copy link

juanazam commented Dec 1, 2016

@moxide I'm facing the same issue, I guess this could be handled in two ways, we can either modify joinChannel to return something like { channel: channel, joinObject: channel.join} or we can include a handler function on the joinChannel function definition and add an "ok" handler within the method.

  joinChannel(name, params, okHandler) {
    ...
    channel
      .join()
      .receive("ok", okHandler);
    return channel;
  }

Let's see what's @mike-north take on this.

@fpalluel
Copy link
Author

fpalluel commented Dec 8, 2016

Hi,
Actually the channel object is already present in the object returned by channel.join() ;-)
The 'okHandler' method looks prettier, thanks !

@juanazam
Copy link

juanazam commented Dec 8, 2016

Yes, I know the returned object contains the channel, but if we change the return value of the joinChannel function, users will have to modify their code, the api would change.

@fpalluel
Copy link
Author

fpalluel commented Dec 9, 2016

ok, got it ;-)

@greyhwndz
Copy link

any say on this? @mike-north

@bkbooth
Copy link

bkbooth commented Dec 19, 2017

I just ran into this issue. I ended up writing my own joinChannel() method in my service that extends PhoenixSocket and made it return a Promise so that I can know if the connection worked or not:

joinChannel(name, params) {
  return new Ember.RSVP.Promise((resolve, reject) => {
    const socket = get(this, 'socket')
    Ember.assert('Must connect to a socket first', socket)

    const channel = socket.channel(name, params)
    channel.join()
      .receive('ok', () => resolve(channel))
      .receive('error', (message) => reject(new Error(message)))
  })
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants