Skip to content

Relay / Gateway

You can create relay to forward data from a protocol to another.

For exemple you have a BLE only device and you would to forward packets from your wifi.

Create your relay

let relay: ComRelay = new ComRelay();

// Configure source and target protocol
let sourceProtocol: ComProtocol = ...; // Your target protocol object instance (could be any of the supported protocols: ble, socket, websocket, nfc...)
let targetProtocol: ComProtocol = ...; // Your target protocol object instance (could be any of the supported protocols: ble, socket, websocket, nfc...)

relay.setTargetProtocol(targetProtocol);
relay.setSourceProtocol(sourceProtocol);

// Start your relay
await relay.start({
    timeout: 5000 // it will throw a timeout exception if it does not start after 5 seconds
});

console.log('Relay is now started');

Socket/Websocket server

If you are using sockets or websockets as a source protocol, you will need to have a socket/websocket server to listen and sends requests.

It is a very basic server that will broadcast any messages to other clients.

// Import packages
import { RelayServerOptions } from "@iotize/device-client.js/relay/api";
import { RelayServer } from "@iotize/device-client.js/relay/impl";

// For websocket server 
import { WebSocketAdapter } from "@iotize/device-com-websocket.js/relay";

// For socket server 
import { SocketAdapter } from "@iotize/device-com-socket.node/relay";

// Create the server 
let options: RelayServerOptions = {

};
let relayServer: RelayServer = new RelayServer(new WebSocketAdapter.Server(), options);

Notes: You can also implement your own adapter as long as it implements interface ServerAdapter interface.

Architecture of relay with sockets

Socket relay architecture