Skip to content

Conversation

xhuberty
Copy link

@xhuberty xhuberty commented Sep 9, 2025

Fix WebSocket API inconsistency causing "this._ws.on is not a function" error

The Gremlin JavaScript driver was mixing browser WebSocket API (.addEventListener)
with Node.js WebSocket API (.on) in the same connection logic. This caused runtime
errors when globalThis.WebSocket was detected but the browser WebSocket implementation
doesn't support the .on() method used for 'unexpected-response' events.

Changes:

  • Conditionally attach 'unexpected-response' listener only for Node.js WebSocket (ws package)
  • Preserve existing useGlobalWebSocket logic for legitimate browser/polyfill scenarios
  • Update cleanup method to safely remove listeners based on WebSocket type

This maintains backward compatibility while fixing the cross-platform API incompatibility
that prevented connections in environments with WebSocket polyfills or global WebSocket objects.

This is related to https://issues.apache.org/jira/browse/TINKERPOP-3160

Fix WebSocket API inconsistency causing "this._ws.on is not a function" error

  The Gremlin JavaScript driver was mixing browser WebSocket API (.addEventListener)
  with Node.js WebSocket API (.on) in the same connection logic. This caused runtime
  errors when globalThis.WebSocket was detected but the browser WebSocket implementation
  doesn't support the .on() method used for 'unexpected-response' events.

  Changes:
  - Conditionally attach 'unexpected-response' listener only for Node.js WebSocket (ws package)
  - Preserve existing useGlobalWebSocket logic for legitimate browser/polyfill scenarios
  - Update cleanup method to safely remove listeners based on WebSocket type

  This maintains backward compatibility while fixing the cross-platform API incompatibility
  that prevented connections in environments with WebSocket polyfills or global WebSocket objects.
@xhuberty
Copy link
Author

The test that fails is due to existing code that I have kept. Do you want to keep it or should I remove it?

this._ws?.off('unexpected-response', this.#handleUnexpectedResponse);
// Only remove unexpected-response listener for Node.js WebSocket (ws package)
// Browser WebSocket does not have this event and .off() method
if (this._ws && 'off' in this._ws) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if the same check as above !useGlobalWebSocket would also work here or is there a particular reason to use different if conditions for .on vs .off?

@andreachild
Copy link
Contributor

The test that fails is due to existing code that I have kept. Do you want to keep it or should I remove it?

Which test is failing exactly? I can't tell from the logs. It seems like the change is backwards compatible so I am wondering what is failing.

@xhuberty
Copy link
Author

The test that fails is due to existing code that I have kept. Do you want to keep it or should I remove it?

Which test is failing exactly? I can't tell from the logs. It seems like the change is backwards compatible so I am wondering what is failing.

lib/driver/connection.ts(477,7): error TS2578: Unused '@ts-expect-error' directive.

@andreachild andreachild changed the base branch from master to 3.7-dev September 16, 2025 23:29
@andreachild andreachild changed the base branch from 3.7-dev to master September 16, 2025 23:30
@andreachild
Copy link
Contributor

Hi @xhuberty can you please change the PR to rebase and target 3.7-dev branch?

@andreachild
Copy link
Contributor

The test that fails is due to existing code that I have kept. Do you want to keep it or should I remove it?

Which test is failing exactly? I can't tell from the logs. It seems like the change is backwards compatible so I am wondering what is failing.

lib/driver/connection.ts(477,7): error TS2578: Unused '@ts-expect-error' directive.

Yes that should be removed as @ts-expect-error is unnecessary after your changes

@xhuberty
Copy link
Author

I will update the PR next week.

@andreachild
Copy link
Contributor

I will update the PR next week.

Hi @xhuberty there is another open PR which has incorporated your changes with modifications #3213. I'd like to be able to merge yours first so that you are acknowledged as the commit author of the fix. Do you know when you'll be able to get back to this one?

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

Successfully merging this pull request may close these issues.

2 participants