JS library providing websocket communication for Zdrowieton hackathon.
Information about the hackathon itself can be found here.
This simple library which enables websocket communication with server.
Just install it using NPM:
npm install zdrowieton
and then require it in your app:
var ZdrowietonMessagingClient = require('zdrowieton')
If you don't use a module bundler like Browserify or Wwebpack, then download this repo and include zdrowietonMessagingClient.js
in your HTML file. Remember to include stomp-js dependency as well!
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.0.0/sockjs.min.js"></script>
<script src="zdrowietonMessagingClient.js"></script>`
First create an instance:
var mc = new ZdrowietonMessagingClient(config);
Where config
is an configurartion with following keys:
-
appId
[string, required] - your unique application ID -
wsUrl
[string, optional] - a URL of the alternative WebSocket server
not required, subscribe and sendMessage automatically connects to server
mc.connect();
mc.subscribe(topicName, function callback(message) { ... });
mc.sendMessage(topicName, message);
A message
can a string
, Array
or an Object
, which will be serialized to JSON automatically
mc.unsubscribe(topicName);
mc.closeConnection();
Check example-browser.html
or example-node.js
for sample usage.