Skip to content
File

Blob: samples/eventsource/worker.js

javascript32 lines
1// Copyright (c) 2017-2024 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5export default {
6 async fetch(req, env) {
7 const ev = new EventSource('http://localhost:8888');
8 
9 // Note that as a non-standard extension, within workers it is possible to
10 // create an EventSource from an existing ReadableStream.
11 //
12 // const ev = EventSource.from(readable);
13 
14 ev.onopen = () => {
15 console.log('open!');
16 };
17 
18 ev.onerror = (event) => {
19 console.log('error!', event.error);
20 };
21 
22 ev.onmessage = (event) => {
23 console.log('message!', event.data, event.lastEventId);
24 };
25 
26 // We'll keep the connection open and running for 20 seconds...
27 await scheduler.wait(20000);
28 
29 return new Response("Hello World\n");
30 }
31};