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

feat(dev): expose APIs for client-server communication #7437

Merged
merged 11 commits into from Mar 26, 2022

Conversation

antfu
Copy link
Member

@antfu antfu commented Mar 24, 2022

Description

Vite as a server really opens up a lot of DX improvements we could make. However, currently, it only allows clients to receive updates from the server but not another way around. To let the client communicate with the server, plugins often need to register a custom middleware to handle the messages as HTTP requests. Which requires quite some effort and could break with various base, port configs with a different environment.

For examples:

Adding this import.meta.hot.send() API allows plugins and libraries to communicate with the server much easier.

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@antfu antfu added feat: hmr p2-nice-to-have Not breaking anything but nice to have (priority) labels Mar 24, 2022
@patak-dev
Copy link
Member

patak-dev commented Mar 24, 2022

+1, vite-plugin-terminal would also benefit from this feature.

About import.meta.hot.send(...), should we use instead import.meta.vite.send(...)? This feature isn't related to hmr, no?
Or maybe import.meta.dev.send if we want a name that others tools could use

@antfu
Copy link
Member Author

antfu commented Mar 24, 2022

import.meta.vite.send sounds like a good idea. If we do so, we might be able to directly expose the socket and other info as well?

@patak-dev
Copy link
Member

Yes, probably some events could also be exposed in import.meta.vite.on(). 'vite:error' for example, information about the module graph also could be interesting?

@antfu
Copy link
Member Author

antfu commented Mar 24, 2022

Guess we could discuss the general idea of import.meta.vite in the next meeting. If we want the ability for client-server communication, I'd also like to see the support on the server-side for handling the WebSocket connections. And ideally, a standardized way to pass custom data. (so we could even hook birpc with it :P)

@patak-dev
Copy link
Member

For reference, we talked about the proposal and we are going to move forward with import.meta.hot.send(). Moving forward we should interpret hot as "APIs related to the dev server".
We should add an example about how to receive the message on the server as part of the new docs.

@antfu antfu marked this pull request as draft March 26, 2022 07:10
@antfu
Copy link
Member Author

antfu commented Mar 26, 2022

Working on the server api

@antfu antfu changed the title feat(client): expose hot.send api for client server communication feat(dev): expose APIs for client-server communication Mar 26, 2022
@antfu antfu marked this pull request as ready for review March 26, 2022 08:23
docs/guide/api-plugin.md Outdated Show resolved Hide resolved
@patak-dev
Copy link
Member

I'm wondering if the hot.send API should match the send server-side API. So instead of hot.send(event, payload), using:

hot.send({ type?, event?, data })

type could default to 'custom', so in general we will be using hot.send({ event, data }). This would leave the door open to send Vite-specific events like hot.send({ type: 'restart' }) (that could be useful for debug utils).

I saw that you already added the 'custom' type in the messageBuffer. Or are you thinking we could have prefixed events like hot.send('vite:restart', payload) in the future?

docs/guide/api-hmr.md Outdated Show resolved Hide resolved
docs/guide/api-plugin.md Outdated Show resolved Hide resolved
antfu and others added 2 commits March 26, 2022 19:53
Co-authored-by: patak <matias.capeletto@gmail.com>
docs/guide/api-hmr.md Outdated Show resolved Hide resolved
patak-dev
patak-dev previously approved these changes Mar 26, 2022
@patak-dev patak-dev merged commit e29ea8e into main Mar 26, 2022
@patak-dev patak-dev deleted the feat/client-ws-send branch March 26, 2022 14:42
@davay42
Copy link

davay42 commented Mar 27, 2022

Great improvement! I've been thinking of a Vite-based CMS for years and this makes it much more real!

@bluwy bluwy mentioned this pull request Apr 4, 2022
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: hmr p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants