Websocketification Client in the fetch
way with the help of WebpackJs
.
- Tests.
- Docs.
- Connection Retry.
npm install --save websocketification-client
Integrate websocketification-client
in the following way and pack it with WebpackJs
before execute it in the browser.
const WebsocketificationClient = require('websocketification-client');
const client = new WebsocketificationClient('ws://127.0.0.1:3123/');
client.connect();
const fetch = client.fetch;
let options = {
method: 'POST',
credentials: 'include',
headers: {'Content-Type': 'application/json'},
body: {name: 'Tom'}
};
return fetch('/users', options).then(
response => response.json()
).then(response => {
console.log('Got users: ', response);
}).catch(error => {
console.error('Failed to get users: ', error);
});
/**
* Add on broadcast listener.
*/
client.setOnBroadcastListener('app.messages', (error, message) => {
if (error) {return console.error(error);}
console.log(message);
});
To run in the server, use ws
as the global.WebSocket
Object.
@see ./examples/get-started.js
.
if ('undefined' === typeof(window) && !global.WebSocket) {
global.WebSocket = require('ws');
}
// ...