Moving forward to device-to-device and device-to-mobile communication in Ombori Grid we are introducing a new concept: spaces
A space is a separate communication channel for devices. Screen and IoT apps and modules can publish/subscribe to messages to/from any space.
Example, sending a message to the space names somespace
in a screen app:
export const App = () => { const pub = usePublish(); const send = useCallback(() => { pub('somespace/Some.MessageType', {payload: "goes here"}); }, [pub]); ... return <button onClick={send} /> }
As you can see, to send a message to a space, just prefix it with the name of the space and add slash.
Note that you can still send messages without prefix, they will be sent to the default space (which is internally called message
so you don't need to modify any existing code.
Subscribing to messages is done in similar fashion:
export const App = () => { const [data, setData] = useState<any>(null); useSubscribe('somespace/Another.MessageType', (payload) => { setData(payload); }, [setData]) return <div>{data}</div> }
So, to subscribe to a message in somespace
, just add a prefix to message type when calling useSubscribe
Same rules apply to modules/IoT apps. When communicating with the default space, don't use the prefix. When talking to a space, add space name before an event type.
Usage
The idea behind spaces is messages are processed differently in different spaces:
message
- this is default space, apps and modules on the same device can communicate with each other thru it. Messages do not leave the device.localnet
- messages will be automatically send to any GridOS devices in the same local network. This requiresmessaging
module.remote
- messages will be forwarded to a connected mobile remote app. Mobile remote app cannot write to the default space, it can only useremote
. Requiresmessaging
module, option must be enabled in module settings.mobileapp
- messages will be forwarded to a local mobile app. Local mobile app cannot write to the default space, it can only usemobileapp
. Requiresmobileapp
module.pubnub
- messages are broadcasted between devices via pubnub service. Requiresmessaging
module. PubKey and SubKey settings should be entered in module settings on each connected device.