socket-request-client

server <-> client connection with ease

Usage no npm install needed!

<script type="module">
  import socketRequestClient from 'https://cdn.skypack.dev/socket-request-client';
</script>

README

socket-request-client

server <-> client connection with ease

INSTALL

npm i --save socket-request-client

USAGE

import clientConnection from 'socket-request-client';
const request = {url: 'user', params: {password: 'password', email:: 'email'}};

const client = await clientConnection('ws://localhost:4000')
// a request is client.on & client.send combined
const requested = await client.request(request);

// without async await
clientConnection('ws://localhost:4000').then(client => {  
  client.on('send', result => { console.log(result) });
  client.send(request);
})

custom pubsub

import clientConnection from 'socket-request-client';
import IpfsApi from 'ipfs-api';
const ipfs = new IpfsApi();

const request = {url: 'user', params: {password: 'password', email:: 'email'}};

const client = clientConnection('ws://localhost:4000', 'echo-protocol', {
  pubsub: ipfs.pubsub,
  retry: false
}).then(client => {
    client.on('send', result => { console.log(result) });
    client.send(request);
  });

API

socketRequestClient([options])

send

request.url
request.params: Object

client.send(request)

request

request.url
request.params: Object
returns: Promise

await client.request(request)

subscribe (local pubsub)

name: name of the channel to subscribe to
handler: method
context: context

client.subscribe('event-name', data => {
  console.log(data);
})

unsubscribe (local pubsub)

name: name of the channel to unsubscribe
handler: method
context: context

client.unsubscribe('event-name', data => {
  console.log(data);
})

publish (local pubsub)

name: name of the channel to publish to
handler: method
context: context

client.publish('event-name', 'data')

uptime

await client.uptime()

pubsub.subscribe

name: name of the channel to subscribe to
handler: method
context: context

client.pubsub.subscribe('event-name', data => {
  console.log(data);
})

pubsub.unsubscribe

name: name of the channel to unsubscribe
handler: method
context: context

client.pubsub.unsubscribe('event-name', data => {
  console.log(data);
})

pubsub.publish

name: name of the channel to publish to
handler: method
context: context

client.pubsub.publish('event-name', 'data')

pubsub.subscribers

client.pubsub.subscribers()

server.uptime

returns: Promise

await client.server.uptime()

server.ping

returns: Promise

await client.server.ping()