GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - NucleoidAI/node-event: Event-driven Message Broker

BranchesTags
Open more actions menu

Latest commit

 

History

32 Commits
32 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
client
client
 
 
examples
examples
 
 
sample
sample
 
 
server
server
 
 
src
src
 
 
.eslintrc.json
.eslintrc.json
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
babel.config.json
babel.config.json
 
 
docker-compose-oracle.yml
docker-compose-oracle.yml
 
 
docker-compose.yml
docker-compose.yml
 
 
index.js
index.js
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 
pub_test.js
pub_test.js
 
 
test.js
test.js
 
 
tsconfig.json
tsconfig.json
 
 

Repository files navigation

react-event

License NPM Discord

Banner

Event-driven Alternative to React Context


npm i @nucleoidai/react-event

react-event is an alternative to React Context with event-driven style communication that helps to build loosely coupled components.

How it works?

Subscribers are registered an event with the custom hook useEvent(eventType, initialValue), once publisher posts an event and its payload, react-event asynchronously sends the event to subscribed components and subscribed components will eventually be re-rendered with fresh data.

Example:

import { useEvent } from "@nucleoidai/react-event";

const Component1 = () => {
  const [event] = useEvent("BUTTON_CLICKED", { number: 10 });

  return <div>{event.number}</div>;
};
import { useEvent } from "@nucleoidai/react-event";

const Component2 = () => {
  const [event] = useEvent("BUTTON_CLICKED", { string: "blue" });

  return <div>{event.string}</div>;
};
import { publish } from "@nucleoidai/react-event";

const PublishComponent = () => {
  return (
      <button
          onClick={() => {
            publish("BUTTON_CLICKED", { number: 11, string: "red" });
          }}
      >
        Button
      </button>
  );
};

Sample react-event

The complete sample project is here.

Stateless Handling

react-event supports stateless components with caching last published payload for the event type, so that if the component is re-rendered, it won't lose the payload. For example, Component 3 in this example is not re-rendered yet, but react-event holds the last payload for the event type, and once the component is rendered, it returns the payload instead of initial value.

react-event Diagram

Event-driven Architecture

Event-driven Architecture is commonly used in Microservices systems that pretty much targets similar problem; loose coupling. This style of architecture require middleware like Kafka, RabbitMQ etc. and we are trying to adopt the very same idea to React.js, of course with some modification such as "Stateless Handling".

API

const [ event ] = useEvent ( eventType , initialValue )

React Hook is to subscribe an event. If there is no event posted yet, it returns initialValue, otherwise, returns last published payload for the event type from cache.

publish ( eventType, payload )

Publish function to post ane event and its payload.

subscribe ( type , callback )

Subscribe function acts like useEvent for non-React JavaScript.

About

Event-driven Message Broker

Topics

event-drivenjavascriptnodejs

Resources

Readme
Apache-2.0 license
Activity
Custom properties

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.