-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
359 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ dist | |
lib | ||
.nuxt | ||
.output | ||
docs/**/*.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
--- | ||
icon: cib:socket-io | ||
--- | ||
|
||
# WebSockets | ||
|
||
> H3 has built-in support for cross platform WebSocket and SSE. | ||
H3 natively supports runtime agnostic [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) API using [CrossWS](https://crossws.unjs.io/). | ||
|
||
::tip | ||
You can define WebSocket handlers in your exisiting [Event Handlers](/guide/event-handler) to define multiple websocket handlers, dynamically matched with your same route defenitions! | ||
:: | ||
|
||
:read-more{title="WebSocket in MDN" to="https://developer.mozilla.org/en-US/docs/Web/API/WebSocket"} | ||
|
||
:read-more{title="CrossWS" to="https://crossws.unjs.io/"} | ||
|
||
> [!IMPORTANT] | ||
> WebSockets support is currently experimental and available in [nightly channel](/guide/nightly). | ||
## Usage | ||
|
||
### Example | ||
|
||
> [!TIP] | ||
> Yuu can use `npx listhen --ws -w websocket.ts` to run this example | ||
<!-- automd:file code src="../../examples/websocket.ts" --> | ||
|
||
```ts [websocket.ts] | ||
import { createApp, defineEventHandler, defineWebSocketHandler } from "h3"; | ||
|
||
export const app = createApp(); | ||
|
||
app.use( | ||
defineEventHandler(() => | ||
fetch( | ||
"https://raw.githubusercontent.com/unjs/crossws/main/examples/h3/public/index.html", | ||
).then((r) => r.text()), | ||
), | ||
); | ||
|
||
app.use( | ||
"/_ws", | ||
defineWebSocketHandler({ | ||
open(peer) { | ||
console.log("[ws] open", peer); | ||
}, | ||
|
||
message(peer, message) { | ||
console.log("[ws] message", peer, message); | ||
if (message.text().includes("ping")) { | ||
peer.send("pong"); | ||
} | ||
}, | ||
|
||
close(peer, event) { | ||
console.log("[ws] close", peer, event); | ||
}, | ||
|
||
error(peer, error) { | ||
console.log("[ws] error", peer, error); | ||
}, | ||
}), | ||
); | ||
|
||
``` | ||
|
||
<!-- /automd --> | ||
|
||
## Server Sent Events (SSE) | ||
|
||
As an alternative to WebSockets, you can use [Server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events). | ||
|
||
H3 has a built-in API to create server-sent events using `createEventStream(event)` utility. | ||
|
||
> [!IMPORTANT] | ||
> SSE support is currently experimental and available in [nightly channel](/guide/nightly). | ||
### Example | ||
|
||
<!-- automd:file code src="../../examples/server-sent-events.ts" --> | ||
|
||
```ts [server-sent-events.ts] | ||
import { createApp, createRouter, eventHandler, createEventStream } from "h3"; | ||
|
||
export const app = createApp(); | ||
|
||
const router = createRouter(); | ||
app.use(router); | ||
|
||
router.get( | ||
"/", | ||
eventHandler((event) => { | ||
const eventStream = createEventStream(event); | ||
|
||
// Send a message every second | ||
const interval = setInterval(async () => { | ||
await eventStream.push("Hello world"); | ||
}, 1000); | ||
|
||
// cleanup the interval and close the stream when the connection is terminated | ||
eventStream.onClosed(async () => { | ||
clearInterval(interval); | ||
await eventStream.close(); | ||
}); | ||
|
||
return eventStream.send(); | ||
}), | ||
); | ||
|
||
``` | ||
|
||
<!-- /automd --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { createApp, defineEventHandler, defineWebSocketHandler } from "h3"; | ||
|
||
export const app = createApp(); | ||
|
||
app.use( | ||
defineEventHandler(() => | ||
fetch( | ||
"https://raw.githubusercontent.com/unjs/crossws/main/examples/h3/public/index.html", | ||
).then((r) => r.text()), | ||
), | ||
); | ||
|
||
app.use( | ||
"/_ws", | ||
defineWebSocketHandler({ | ||
open(peer) { | ||
console.log("[ws] open", peer); | ||
}, | ||
|
||
message(peer, message) { | ||
console.log("[ws] message", peer, message); | ||
if (message.text().includes("ping")) { | ||
peer.send("pong"); | ||
} | ||
}, | ||
|
||
close(peer, event) { | ||
console.log("[ws] close", peer, event); | ||
}, | ||
|
||
error(peer, error) { | ||
console.log("[ws] error", peer, error); | ||
}, | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.