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.

How to use "ws.once" with this library?

See original GitHub issue

Hello!

I adopted this package when I was needing to have my WS connections restarted if they disconnect. It worked perfectly in my case, since I was using this to follow up crypto-currencies value changes.

Now I wish to also have a channel open, using this package, but I lack one functionality that my code needs, which is the method: “.once("message",() => {})”. I use this to send crypto orders and get their respective message right after.

Anyone have ideas on how to achieve that?

I need to follow one pipeline of: send data to ws server -> get the message and send new data -> get message and send -> … and so on until I achieve the end(I usually do this around 4 times), which is a variable full of specific data for my app.

Searching through the “reconnecting-websocket” lib I found no “once” method.

I really appreciate if anyone can help me.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
makyencommented, Jan 7, 2022

@regisfaria You haven’t mentioned the actual environment in which you are running your code, so there’s no way for me to know that or address any issues. There’s also no way for me to determine if your IDE is providing you with the actual signature for the method being called, which it may or may not be accurately displaying (i.e. such things are wrong from time to time, don’t assume 100% accuracy).

If you’re using Node.js, then it looks like {once: true} was added in 14.5.0, which was released on 2020-06-30. If you’re using Deno, then {once: true} was added in 1.0, which was released on 2020-05-13. Beyond those, I don’t have much information.

Reconnecting-websocket merely uses .bind() to use all features of the underlying event features:

        // Expose the API required by EventTarget

        this.addEventListener = eventTarget.addEventListener.bind(eventTarget);
        this.removeEventListener = eventTarget.removeEventListener.bind(eventTarget);
        this.dispatchEvent = eventTarget.dispatchEvent.bind(eventTarget);

So, if the {once: true} feature is available in the environment you’re running in will depend entirely on that environment. Using {once: true} definitely works with reconnecting-websocket in browsers.

As I mentioned, {once: true} is just a convenience. It doesn’t add anything which can’t be done with only .addEventListener() and .removeEventListener(). The other answers on the question to which I linked give multiple examples of how you can accomplish this with just those methods and without .once() or {once: true}.

Overall, I wouldn’t support doing any work within reconnecting-websocket to polyfill support for some type of once feature, given that it already works if available in the underlying environment. If it really isn’t available in the environment you’re using and upgrading the environment isn’t an option, then you could add/load a polyfill (seems like overfill), or just use a few lines of code to implement the feature within the event handlers where you’re wanting to use it.

1reaction
makyencommented, Jan 5, 2022

Something like .once() is merely a convenience function that puts a wrapper around whatever listener you’re adding which removes itself and your listener after a single event. As long as you have a way to add listeners and remove listeners, then there is absolutely no reason you can’t implement a similar feature within your event listener, or even an actual .once(), yourself. There’s no need to push something like that down into a low level implementation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ws: a Node.js WebSocket library - GitHub
ws is a simple to use, blazing fast, and thoroughly tested WebSocket client and server implementation. Passes the quite extensive Autobahn test suite:...
Read more >
Using the 'ws' library on a specific request - Stack Overflow
I would like to use the ws NPM package to write a websocket server. However, the ways ws can be set up is...
Read more >
WebSockets and Node.js - testing WS and SockJS by building ...
First, require the WS library and use the WebSocket.Server method to create a new WebSocket server on port 7071 (no significance, any port...
Read more >
ws.WebSocket.once JavaScript and Node.js code examples
How to use. once. function. in. WebSocket · Best JavaScript code snippets using ws.WebSocket.once(Showing top 6 results out of 315) · Most used...
Read more >
WebSocket - The Modern JavaScript Tutorial
To open a websocket connection, we need to create new WebSocket using the special protocol ws in the url: let socket = new...
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