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

Pipe()'s NodeJS EventEmitter Compatibility #101

Closed
SirNeural opened this issue Aug 15, 2018 · 6 comments
Closed

Pipe()'s NodeJS EventEmitter Compatibility #101

SirNeural opened this issue Aug 15, 2018 · 6 comments

Comments

@SirNeural
Copy link

SirNeural commented Aug 15, 2018

Hi,
First off, this is a great library @dcharbonnier, just what I felt was missing in the original ws library.
I've been trying to implement this in place of ws, but am experiencing difficulties in doing so because there is a slight difference between this and the original node ws library.
In the original node library event listeners can be attached like this:

`
const ws = new WebSocket('wss://server')

ws.on('open', OPEN_CALLBACK)

ws.on('message', MESSAGE_CALLBACK)

....
`

In this library, event listeners must be attached like this, similar to in web browser javascript:

`
const ws = new WebSocket('wss://server')

ws.addEventListener('open', OPEN_CALLBACK)

ws.addEventListener('message', MESSAGE_CALLBACK)

....
`
That alone isn't the cause of my problems, because the callback in the first example for the message event returns the message to the callback, as a string, but the second one returns an event object to the callback, with the message as a property attached to it; This breaks my existing code and would be a big hassle to change.
Since this is a big incompatibility with the node ws library, I think it would be nice if there could be support for .on() and the other node EventEmitter methods, that sends just the property instead of the entire event object (I think just extending from EventEmitter should work, not completely sure)

Thanks again for this great library!

@dcharbonnier
Copy link
Owner

The library stick to the Websocket standard, more than ws. But what you describe is in the Todo list and I will put it on top (next 10 days) knowing that someone else need it. Thanks for your comments.

@kmannislands
Copy link

@SirNeural nice ticket, this helped me figure out what was wrong in my setup.
To patch the interfaces in the meantime (before this fix is published), I basically did:

const EventEmitterCompliantChannel = new Pipe(ws, 'A');

// heres the interface patching:
EventEmitterCompliantChannel.on = (event, cb) =>
  EventEmitterCompliantChannel.addEventListener(event, (msg) => cb(msg.data));

@SirNeural
Copy link
Author

@kmannislands Hey thanks for posting your solution, I did manage to do the same kind of interface patching in order to get this to work in the last few days, nice to see this helped someone out!

@dcharbonnier
Copy link
Owner

I guess you need that for websocket-json-stream, it's also quite easy to replace this library and match the standard websocket client implementation.
It's only one simple file https://github.com/avital/websocket-json-stream/blob/master/index.js.
Eventually this will be the bottling.

@dcharbonnier
Copy link
Owner

WIP #105

@dcharbonnier
Copy link
Owner

merging issue to #20

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

No branches or pull requests

3 participants