Coder Social home page Coder Social logo

arun9483 / sse-notification-client Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 45 KB

ReactJs based CSR website to receive notification message from server using Server-Sent Events (SSE)

License: MIT License

JavaScript 69.59% HTML 5.29% CSS 25.12%

sse-notification-client's Introduction

sse-notification-client

ReactJs based CSR website to receive notification message from server using Server-Sent Events (SSE)

SSE client side code

// Client side code for Server-Sent-Event(SSE)
useEffect(() => {
  /**
   * open SSE connection with server - event-stream
   */
  const eventSource = new EventSource(`${baseUrl}/events`);

  /**
   * read message from event-stream - assign callback function for onmessage event
   * once SSE connection is established server can sent message in the format of key-value pair i.e. res.write(`key:value\n\n`);
   * message of interest can be read from event by it's key i.e. event.data is used to read message sent from server and rest is ignored inside onmessage callback function.
   */
  eventSource.onmessage = (event) => {
    /**
     * read only event's data value that was sent from server using res.write(`data: ${JSON.stringify(newMessage)}\n\n`);
     * Ignore others i.e. res.write(': keep-alive\n\n');
     * `\n\n` in SSE: Used to indicate the end of a message, allowing the client to properly parse each message.
     */
    const newMessage = JSON.parse(event.data);
    setMessages((prevMessages) => [...prevMessages, newMessage]);
  };

  // Cleanup on unmount
  return () => {
    /**
     * close SSE connection with server - event-stream
     */
    eventSource.close();
  };
}, []);

sse-notification-client's People

Contributors

arun9483 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.