0.3.0 • Published 6 years ago

socket.io-acknowledge v0.3.0

Weekly downloads
23
License
-
Repository
github
Last release
6 years ago

socket.io-acknowledge

Adds acknowledgement functions to socket.emit and makes it return a promise which resolves/rejects based on what its corresponding socket.on handler function returns or throws:

  • client

    const value = await socket.emit('example', 123)
  • server

    socket.on('example', async data => {
      return await 'value'
    })

Install

npm i socket.io-acknowledge

Usage

client

const SocketIO = require('socket.io-client')
const acknowledge = require('socket.io-acknowledge')

const socket = SocketIO(SERVER_URL)
acknowledge(socket)

async function example() {
  try {
    /* socket.emit returns acknowledgement promise */
    const value = await socket.emit('example', data)

  } catch (error) {
    /* or throws acknowledgement error */
    console.error(error)
  }
}

server

const SocketIO = require('socket.io')
const acknowledge = require('socket.io-acknowledge')

const io = SocketIO({})
io.use(acknowledge)

io.on('connect', socket => {

  /* socket.on handlers can be async */
  socket.on('example', async data => {
    if (…) {
      /* socket.on handler's return values acknowledged by originating emit functions */
      return value
    } else {
      /* or get thrown by originating emit functions */
      throw new Error(…)
    }
  })

})
0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago