EventSource()
O construtor EventSource()
retorna uma nova EventSource
, que representa um recurso remoto.
Sintaxe
eventSource = new EventSource(url, configuration);
Parâmetros
url
-
Uma
USVString
(en-US) que representa a localização de um recurso remoto servindo os eventos/mensagens. configuration
Optional-
Fornece opções para configurar a nova conexão. Os atributos possíveis são:
withCredentials
, valor padrãofalse
, indicando se o CORS deve ser instruído aincluir
credenciais.
Exemplos
var evtSource = new EventSource('sse.php');
var eventList = document.querySelector('ul');
evtSource.onmessage = function(e) {
var newElement = document.createElement("li");
newElement.textContent = "message: " + e.data;
eventList.appendChild(newElement);
}
Nota: Você pode encontrar um exemplo completo no GitHub — veja Simple SSE demo using PHP.
Especificações
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'EventSource()' in that specification. |
Padrão em tempo real | Definição inicial |
Compatibilidade com navegadores
BCD tables only load in the browser