question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide callback for the new Echo instance (or better yet return a Promise)

See original GitHub issue

Getting the socket id for non vue / axios apps kind of sucks if you don’t want to put the echo instance on the window object. You can only get it once you’ve connected to the socket and echo provides no native functionality (as far as I am aware) to do this reliably.

I had to do this

  echo.connector.pusher.connection.bind('connected', () => {
    console.log(echo.socketId());
  });

Which I learned about from this stackoverflow post and the Pusher docs.

Not ideal. It would be nice if you could just go

let socketId = null;
const echo = new Echo(options).then(echo => {
  socketId = echo.socketId();
});

Even a callback would be enough.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
tlaverdurecommented, Nov 30, 2018

It’s not a common practice to create a promise from a constructor. I’ve added a change in https://github.com/laravel/echo/pull/210 to move the connection logic to it’s own method. If that is merged, I can work on creating a connection event handler from the connectors.

Then we can either add a callback in the options object:

new Echo({
  ...
 onConnect: () => {...}
  ...
})

Or remove the connect() method from the constructor and require the developer to connect at will.

const echo = new Echo(...);

echo.connect().then(() => {
  // connected
});

@driesvints which would work better?

0reactions
driesvintscommented, Jul 31, 2020

Hey all. I’ve been looking into this and while I agree this seems like a good thing to have in the library we’re not going to work on this ourselves at the moment. We’re however open to prs if anyone wants to take a stab at this themselves! Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I return the response from an asynchronous call?
This means when you're returning, the listener you've defined did not execute yet, which means the value you're returning has not been defined....
Read more >
Promises in the AWS SDK for PHP Version 3
Chaining promises​​ Promises are composed by chaining then callbacks with subsequent then callbacks. The return value of a then method is a promise...
Read more >
Using promises - JavaScript - MDN Web Docs
Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function. Imagine a function, ...
Read more >
Promise - event - sabre/dav
This method returns a Promise object. The Promise object is initially pending, but later on it will have the result of the operation....
Read more >
Async JS Crash Course - Callbacks, Promises, Async Await
In this crash course we will look at asynchronous JavaScript and cover callbacks, promises including promise.all as well as the async ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found