Virtual Network Framework
npm install @amid.ukr/virtual-network-framework

js
// Creating in browser hub of endpoints
const vnfHub = new Vnf.InBrowserHub()
// Creating endpoint in hub
const endpoint1 = vnfHub.openEndpoint("endpoint1-name")
const endpoint2 = vnfHub.openEndpoint("endpoint2-name")
// Listening to messages
endpoint1.onMessage = function(event) {
console.info("Endpoint-1 got a message from endpoint " + event.sourceEva + ", message is: " + event.message)
if(event.message == "I am good!") {
console.info("Endpoint 1 is closing connection to " + event.sourceEva)
endpoint1.closeConnection(event.sourceEva)
}
}
endpoint2.onMessage = function(event) {
console.info("Endpoint-2 got a message from endpoint " + event.sourceEva + ", message is: " + event.message)
if(event.message == "How are you?") {
endpoint2.send(event.sourceEva, "I am good!")
}
}
// Handling close connection and releasing endpoints
endpoint2.onConnectionLost(function(targetEva){
console.info("Endpoint-2 lost connection to " + targetEva + ", releasing endpoints")
endpoint1.destroy()
endpoint2.destroy()
})
// Open connection and sending handshake message
endpoint1.openConnection("endpoint2-name", function(event){
if(event.status != "CONNECTED") return;
console.info("Endpoint-1 established connection to " + event.targetEva + ", status is: " + event.status)
endpoint1.send(event.targetEva, "How are you?")
})
`
$3
`js
// Creating in browser hub of endpoints
const vnfHub = new Vnf.WebSocketHub(new Vnf.WebSocketFactory("wss:///webbroker/vnf-ws"));
// Creating endpoint in hub
const endpoint1 = vnfHub.openEndpoint("endpoint1-name")
const endpoint2 = vnfHub.openEndpoint("endpoint2-name")
// Rest part the same as InBrowserHub
...
`
$3
`js
// Creating in browser hub of endpoints
const webSocketFactory = new Vnf.WebSocketFactory("wss:///webbroker/vnf-ws")
const vnfHub = new Vnf.WebSocketHub(webSocketFactory);
// Creating endpoint in hub
const endpoint1 = vnfHub.openEndpoint("endpoint1-name")
const endpoint2 = vnfHub.openEndpoint("endpoint2-name")
// Rest part the same as InBrowserHub
...
`
$3
`js
// Creating in browser hub of endpoints
const webSocketFactory = new Vnf.WebSocketFactory("wss:///webbroker/vnf-ws")
const vnfHub = new Vnf.RtcHub(new Vnf.WebSocketHub(webSocketFactory));
// Creating endpoint in hub
const endpoint1 = vnfHub.openEndpoint("endpoint1-name")
const endpoint2 = vnfHub.openEndpoint("endpoint2-name")
// Rest part the same as InBrowserHub
...
`
$3
`js
// Creating in browser hub of endpoints
const webSocketFactory = new Vnf.WebSocketFactory("wss:///webbroker/vnf-ws")
const vnfHub = new Vnf.MarshallerHub(new Vnf.ReliableRtcHub((webSocketFactory)));
// Creating endpoint in hub
const endpoint1 = vnfHub.openEndpoint("endpoint1-name")
const endpoint2 = vnfHub.openEndpoint("endpoint2-name")
// Rest part the same as InBrowserHub
...
`
VNF Registry
VNF Registry is used for discovery.
For example chat application can use it can register for chat channel, so other user can use registry to find active chats to connect. Registry entry will remain available until client channel will be closed or lost.
Two registries implementation are supported:
- InBrowserRegistry - registry in browser memory, used for testing and mocking
- WebSocketRegistryClient - registry client over websocket protocol
Examples
$3
`js
const sharedRegistry = new Vnf.InBrowserRegistry();
const registryClient = sharedRegistry.connect("endpoint1-name")
Promise.resolve()
.then(registryClient.createEntry.bind(null, {collection: collection1Name, name:"entry1"}, "entry value"))
.then(function(status) {
assert.equal(status, Vnf.Global.OK, "asserting status");
})
.then(registryClient.getEntry.bind(null, {collection: collection1Name, name:"entry1"}))
.then(function(value){
assert.equal(value, "entry value", "asserting inserted entry");
})
.then(done);
`
$3
`js
const webSocketFactory = new Vnf.WebSocketFactory("wss:///webbroker/vnf-ws")
const registryClient = new Vnf.WebSocketRegistryClient(new Vnf.WebSocketRpc(eva, webSocketFactory))
// Rest part the same as InBrowserRegistry
...
`
$3
`js
const webSocketFactory = new Vnf.WebSocketFactory("wss:///webbroker/vnf-ws")
const vnfHub = new Vnf.WebSocketHub(webSocketFactory);
// Creating endpoint in hub
const endpoint1 = vnfHub.openEndpoint("endpoint1-name")
const registryClient = new Vnf.WebSocketRegistryClient(endpoint1.getWebSocketRpc())
// Rest part the same as InBrowserRegistry
...
``