JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://unpkg.com/@wsrpc/client"></script>
JavaScript
var RPC = new WSRPC('wss://demo.wsrpc.info/ws/', 5000);
// Register client route, that can be called by server.
// It would be called by server, when server sends a joke.
RPC.addRoute('joke', function (data) {
// After server sends a joke server waits for an answer, if joke is
// funny. test.getJoke call is going to be finished only after user
// sends response.
return confirm(data.joke + '\n\nThat was funny?');
});
RPC.connect();
// Request server to tell a joke by calling test.getJoke
// Server would call client route 'joke' (registered above) and wait for
// client answer (if joke is funny or not).
RPC.call('test.getJoke', {}).then((result) => {
// Here you would finally see server reaction on your feedback about
// joke.
// If 'joke' client route responds that joke is funny - you would see
// something like 'Cool!', or 'Hmm... try again' if it's not.
alert(result);
}, (error) => {
alert(e.type + '("' + e.message + '")');
});