Web APIs
EventSource()
Opens a server-sent event stream; receives text events and reconnects according to the processing model.
Formal Syntax & Structure
javascript syntaxjavascript
new EventSource(url, [options])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| url | string | URL | Event-stream endpoint. |
| options | object | Optional withCredentials setting. |
Return Value
EventSource
Opens a server-sent event stream; receives text events and reconnects according to the processing model.
Working Example
Example Snippetjavascript
(async () => {const es=new EventSource("/events");try{const event=await new Promise((ok,no)=>{es.addEventListener("update",ok,{once:true});es.onerror=()=>no(new Error("stream failed"));});console.log(event.lastEventId,event.data);}finally{es.close();}})()