evanw / evanw/esbuild

Expose event source clients

Open
#3,587 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Is some way esbuild could expose if there are currently browser(s) connected?

One use case would be to open a new browser window conditionally, but only if there are no clients connected:

```js
const {port, /* hypothetical API -> */ clients} = await context.serve({port: 3000});

// open in browser if we don't already have an open browser
if (await clients().length === 0) open(`http://localhost:${port}`);
console.log(`frontend: server listening on port ${port}`);
```

This could, hypothetically, be extended to allow sending arbitrary messages to listening clients, enabling more advanced reload scenarios, error message overlays, etc:

```js
for (const client in await clients()) client.postMessage('hi!')
```

and even listen for messages from the client:

```js
const onMsg = data => console.log(data)
for (const client in await clients()) client.addEventListener('message', onMsg)
```

thoughts?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing esbuild's context.serve API and the browser client behavior described in the examples. Determine the scope of exposing connected clients, including whether messaging and client events belong in the same change; done would include a documented API with behavior that supports checking connected browsers.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, javascript
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.