Web APIs

EventSource()

Try the free JavaScript compiler

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

NameTypeDescription
urlstring | URLEvent-stream endpoint.
optionsobjectOptional 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();}})()
JavaScript Reference