Skip to content

Server sent events(SSE) is a pushing technology that enables pushing notification/message/events from the server to the client(s) via HTTP connection. (Angular + Node)

Notifications You must be signed in to change notification settings

Rubanrubi/sse-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

sse-node

In Angular how to consume SSE ?

Create a service file in that file add the below function

getServerSentEvent(url: string): Observable { return new Observable(observer => { const eventSource = new EventSource(url);

  eventSource.onmessage = (event) => {
    observer.next(event.data);
  };

  eventSource.onerror = (error) => {
    observer.error(error);
  };

  return () => {
    eventSource.close();
  };
});

}

Component file

Add the state variables

messages: string[] = [];
subscription: Subscription;

Inside constructor or onInit subscribe for the event

 this.subscription = this.sseService.getServerSentEvent('https://localhost:3000/events')
.subscribe((data: string) => {
console.log('event', data);
  this.messages.push(data);
});

About

Server sent events(SSE) is a pushing technology that enables pushing notification/message/events from the server to the client(s) via HTTP connection. (Angular + Node)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published