feat(ws): expose on
/ off
for server.ws
#5273
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Expose
on
/off
listeners forserver.ws
for plugins to leverage the events.Additional context
The main motivation it that the current interface of
server.ws
is too limited. Let's say if we have a virtual module that needs HMR frequently, I usually mock thews
events by sending (since the virtual module will not have the fs event):The problem is that it will only be received by clients that already established connections. The first client might get the initial content of the virtual module by the
load
hook and processed to load other modules, and then after a while the was connect gets established. This means the ws events during the period won't be received by the client and resulting in the old virtual module get served. This requires users to manually refresh the page to get the latest or having an inaccurate timeout to do the hard refresh.load
--->hmr event
(lost) --->ws connected
---> page ready (the old content is served)Having the listeners exposed allowing plugins to pending the events and send them as soon as the client is ready (e.g.
on('connection')
to make the HMR experience seamless.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).