--- name: Build a publish-subscribe WebSocket server --- Bun's server-side `WebSocket` API provides a native pub-sub API. Sockets can be subscribed to a set of named channels using `socket.subscribe()`; messages can be published to a channel using `socket.publish(, )`. This code snippet implements a simple single-channel chat server. ```ts const server = Bun.serve<{ username: string }>({ fetch(req, server) { const cookies = req.headers.get("cookie"); const username = getUsernameFromCookies(cookies); const success = server.upgrade(req, { data: { username } }); if (success) return undefined; return new Response("Hello world"); }, websocket: { open(ws) { const msg = `${ws.data.username} has entered the chat`; ws.subscribe("the-group-chat"); ws.publish("the-group-chat", msg); }, message(ws, message) { // the server re-broadcasts incoming messages to everyone ws.publish("the-group-chat", `${ws.data.username}: ${message}`); }, close(ws) { const msg = `${ws.data.username} has left the chat`; ws.publish("the-group-chat", msg); ws.unsubscribe("the-group-chat"); }, }, }); console.log(`Listening on ${server.hostname}:${server.port}`); ``` tifact-4 Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/src (unfollow)
AgeCommit message (Collapse)AuthorFilesLines
2018-01-11adapt to changes in the cortex-m crateGravatar Jorge Aparicio 9-39/+66
2018-01-11fix the compiletest_rs versionGravatar Jorge Aparicio 1-1/+1
2018-01-11switch to a stable release of compiletest_rsGravatar Jorge Aparicio 1-2/+1
2018-01-11use master branch of cortex-mGravatar Jorge Aparicio 1-1/+1
2017-12-23add a Cargo feature, cm7-r0p1, to fix a Cortex-M7 BASEPRI erratumGravatar Jorge Aparicio 3-2/+17
2017-12-23switch to a compiletest version that works with the latest nightlyGravatar Jorge Aparicio 1-1/+2
2017-12-23switch to master branchesGravatar Jorge Aparicio 2-3/+3
2017-12-17add missing commaGravatar Jorge Aparicio 1-1/+1
2017-12-09safe `&'static mut` references via init.resourcesGravatar Jorge Aparicio 6-10/+153
2017-12-09implement the Resource trait for owned resourcesGravatar Jorge Aparicio 11-220/+278
this unbreaks the "generics" example
2017-12-09drop the Static wrapperGravatar Jorge Aparicio 8-26/+28
2017-12-09make resource proxies not SyncGravatar Jorge Aparicio 2-3/+2
2017-12-09also cache the Xargo directoryGravatar Jorge Aparicio 1-1/+4
2017-12-09deny warnings and unsafe code in tests and examplesGravatar Jorge Aparicio 24-0/+24
2017-12-09make resource proxies !SendGravatar Jorge Aparicio 2-2/+58
2017-12-09fix the "generics" exampleGravatar Jorge Aparicio 1-2/+2
2017-12-09rename LateResourceValues to LateResourcesGravatar Jorge Aparicio 4-9/+9
2017-12-09remove special case around peripherals from codegenGravatar Jorge Aparicio 1-209/+114
2017-12-09peripherals as scoped singletonsGravatar Jorge Aparicio 7-30/+68
2017-12-08ci: switch back to a recent nightlyGravatar Jorge Aparicio 1-2/+1
2017-12-08fix cfail testsGravatar Jorge Aparicio 2-2/+2
2017-11-22use older nightly for the x86_64 linux targetGravatar Jorge Aparicio 1-1/+2
2017-11-22fix ciGravatar Jorge Aparicio 3-4/+2
2017-11-22v0.2.2v0.2.2Gravatar Jorge Aparicio 10-61/+161
2017-10-02fix the exampleGravatar Jorge Aparicio 1-2/+2
2017-10-02fix `idle::Resources::new`Gravatar Jorge Aparicio 2-2/+13
it assumed that all resources were "early" resources
2017-09-22Use `true` as the armv6 linkerGravatar Jonas Schievink 1-2/+2
Horrible hack until we switch to a Cortex-M0 device crate that works with armv6.
2017-09-22Pin Travis to Xargo 0.3.8Gravatar Jonas Schievink 1-2/+2
2017-09-22Use rtfm-syntax 0.2.0Gravatar Jonas Schievink 1-2/+1