Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Module-to-app communication

Sending messages

A module is able to send messages to an application by invoking an RPC call on GdmAgent module. Reference implementation in JS:

...

Code Block
const gridos = require(‘./gridos.js’gridos’);

...

const client = await gridos.connect();

...

client.broadcast({ type: 'Test.Message', some: 'data' });

Receiving messages in the app

From the app side, @ombori/ga-messaging library should be used. Reference implementation:

Code Block
import { useBroadcast useSubscribe } from ‘@ombori/ga-messaging’;

const App = () => {

   ...
   const {count, setCount} = useState(0);
   const useSubscribe('Test.Message', (msg) => {
     setCount(ct => ct + 1);
   }, [setCount]);

  return <div>Received {count} messages</div>
};

App-to-module communication

To send messages from app to a module, use @ombori/ga-messaging library. Example:

Code Block
import { usePublish } from ‘@ombori/ga-messaging’;

const App = () => {

   ...

   const broadcastpublish = useBroadcastusePublish();
   const onClick = useCallback(() => {
     broadcast     publish({type: ‘Button‘Hello.Clicked’There’, some: ‘data here’}); 
   }, [broadcast]);

  return <button onClick={onClick} />
};

Receiving messages in the module

A module is receiving messages via an inbound RPC call of broadcast method. Reference implementation:

Code Block
client.onMethod(broadcast => (req, res) => {
  try {
    const data = req.payload;
    swtch(data.type) {
      case 'Hello.There':
        console.log('a hello event is received', data);
      break;
    }
    res.send(200, 'ok');
  } catch (e) {
    res.send(500, e.toString())
  }
})

Nodejs-based modules can use gridos library instead:

Code Block
const gridos = require('./gridos');

...

const client = await gridos.connect();

...

client.onEvent('Hello.There', (data) => {
  console.log('a hello event is received', data);
});