Provide callback for the new Echo instance (or better yet return a Promise)
See original GitHub issueGetting 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:
- Created 5 years ago
- Reactions:7
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:Or remove the
connect()
method from the constructor and require the developer to connect at will.@driesvints which would work better?
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.