Skip to content

Latest commit

 

History

History
72 lines (51 loc) · 1.6 KB

File metadata and controls

72 lines (51 loc) · 1.6 KB

Websocketification Client

Websocketification Client in the fetch way with the help of WebpackJs.

TODO

  • Tests.
  • Docs.
  • Connection Retry.

Installation

npm install --save websocketification-client

Browser-Side Usage

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);
});

Server/NodeJs Side Usage

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');
}

// ...