- Create a new JavaScript file called
listener.js
.
- Run
npm install hyco-ws
from a Node command prompt in your project folder.
-
Add the following
constant
to the top of thelistener.js
file.const WebSocket = require('hyco-ws');
-
Add the following Relay
constants
to thelistener.js
for the Hybrid Connection connection details. Replace the placeholders in brackets with the proper values that were obtained when creating the Hybrid Connection.const ns
- The Relay namespace (use FQDN - e.g.{namespace}.servicebus.windows.net
)const path
- The name of the Hybrid Connectionconst keyrule
- The name of the SAS keyconst key
- The SAS key value
-
Add the following code to the
listener.js
file:var wss = WebSocket.createRelayedServer( { server : WebSocket.createRelayListenUri(ns, path), token: WebSocket.createRelayToken('http://' + ns, keyrule, key) }, function (ws) { console.log('connection accepted'); ws.onmessage = function (event) { console.log(event.data); }; ws.on('close', function () { console.log('connection closed'); }); }); console.log('listening'); wss.on('error', function(err) { console.log('error' + err); });
Here is what your listener.js should look like:
const WebSocket = require('hyco-ws'); const ns = "{RelayNamespace}"; const path = "{HybridConnectionName}"; const keyrule = "{SASKeyName}"; const key = "{SASKeyValue}"; var wss = WebSocket.createRelayedServer( { server : WebSocket.createRelayListenUri(ns, path), token: WebSocket.createRelayToken('http://' + ns, keyrule, key) }, function (ws) { console.log('connection accepted'); ws.onmessage = function (event) { console.log(event.data); }; ws.on('close', function () { console.log('connection closed'); }); }); console.log('listening'); wss.on('error', function(err) { console.log('error' + err); });