Phoenix.js reconnect params

Hey guys.

As far as I know, on phoenix.js, the params sent to the server on reconnect are the same when we call connect. Is there an option to update the params for reconnect? My usecase is the following:

  • User visits website as anonymous
  • Server responds with a temp id which is saved on cookie
  • For the next visits, the user reads the cookie and sends the value on connect.

The current reconnect approach works fine for all but the first visit. Once the cookie is set we should reconnect with this value, otherwise the server will generate a new ID.

Thanks for the help :smile:

I posted on phoenix-core mailing about supporting params as a function that returns an object. Not sure if I should/how-to remove this thread.

Anyway, thanks :slight_smile:

There is actually a simple solution to this problem, as Chris McCord pointed out in the mailing list:

let socket = new Socket(“/….”, {params: {…}})
socket.onOpen(() => {
  socket.params = {…} // next rejoin will use new params

Update for Phoenix 1.4, as socket.params is now wrapped inside a closure on the socket object:

socket.onOpen(() => {
  socket.params = () => ({ reconnect: true, … })
1 Like

I’m wondering if this isn’t an implementation detail, which should much rather be hidden behind a proper method to change the params on the socket object.

You are right, I’m just posting my findings here, as I stumbled on this topic while debugging. If Socket gets a setParams function, this could be hidden indeed.

1 Like