npm.io
2.1.3 • Published 6 months ago

socket-request-client

Licence
MIT
Version
2.1.3
Deps
2
Size
42 kB
Vulns
0
Weekly
0

socket-request-client

server <-> client connection with ease

INSTALL

npm i --save socket-request-client

USAGE

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

const client = new SocketRequestClient('ws://localhost:4000')
const connection = await client.init()
// a request is client.on & client.send combined
const requested = await connection.request(request);
// or
client.clientConnection.request(request)
custom pubsub
import {SocketRequestClient} from 'socket-request-client';
import IpfsApi from 'ipfs-api';
const ipfs = new IpfsApi();

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

const client = new SocketRequestClient('ws://localhost:4000', 'echo-protocol', {
  pubsub: ipfs.pubsub,
  retry: false
})
const connection = await client.init()
connection.on('send', result => { console.log(result) })
connection.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()