Skip to content

Websocketification/websocketification-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

// ...

About

Websocketification client in the `fetch` way.

Resources

Stars

Watchers

Forks

Packages

No packages published