# febpop

> [![Build Status](https://circleci.com/gh/justincase-jp/Febpop/tree/master.svg?style=shield)]( https://circleci.com/gh/justincase-jp/Febpop ) [![npm version](https://badge.fury.io/js/febpop.svg)]( https://badge.fury.io/js/febpop )

Latest version **0.2.7** (published 2021-01-09) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install febpop
pnpm add febpop
yarn add febpop
bun add febpop
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.7 |
| Published | 2021-01-09 |
| First published | 2020-02-18 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 20.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Nicholas Yip |
| Maintainers | jict |
| Keywords | Socket.IO, TypeScript, ack, timeout, retry, transaction, at-least-once delivery |

## Links

- npm: https://www.npmjs.com/package/febpop
- Repository: https://github.com/justincase-jp/Febpop
- Homepage: https://github.com/justincase-jp/Febpop#readme
- Issues: https://github.com/justincase-jp/Febpop/issues
- npm.io page: https://npm.io/package/febpop

## Dependencies (2)

- [socket.io-client](https://npm.io/package/socket.io-client.md) ^2.4.0
- [@types/socket.io-client](https://npm.io/package/@types/socket.io-client.md) ^1.4.34

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.2.7 (latest) — 2021-01-09
- 0.2.6 — 2020-07-19
- 0.2.4 — 2020-04-01
- 0.2.2 — 2020-02-18
- 0.2.0 — 2020-02-18

## README

[![Build Status](https://circleci.com/gh/justincase-jp/Febpop/tree/master.svg?style=shield)](
  https://circleci.com/gh/justincase-jp/Febpop
)
[![npm version](https://badge.fury.io/js/febpop.svg)](
  https://badge.fury.io/js/febpop
)

Febpop
===
Febpop is an opinionated wrapper to enforce consistent data transmission within Socket.IO.

The reliability of packet transmission in Socket.IO is guaranteed by the underlying TCP protocol.
Combining with its built-in acknowledgement mechanism, it would seem at first, transactional message deliveries can be
implemented by simplify waiting for acknowledgement responses from the server.

However, in the real world, clients may have poor networks and servers need restarts from time to time.
A client could get stuck in waiting for acknowledgement responses that will never happen.


This library is created to address such a problem by enforcing at-least-once delivery semantics:

* Each emission of messages in this library is preconfigured with a timeout (default: 10 seconds).
* Within the configured timeout, a re-emission will be attempted every time a reconnection takes place, until it is
confirmed as acknowledged from the server.


## Installation

```sh
npm install febpop
```


## Usage
While Socket.IO is a rich library that provides quite an amount of features, this library focuses on an essential
subset. The core interface is simplified as:

```ts
export interface Febpop {
  on<T>(event: string): (action: (argument: T) => void) => void

  emit<T>(event: string, timeout?: number): (argument: T, onTimeOut: () => void) => CompletionHandler
  emit<T>(event: string, timeout: null): (argument: T) => CompletionHandler
}
```

Functions are curried in advance for the ease to define message types. For example:

```ts
import febpop, { Febpop } from 'febpop'

class ApiService {
  constructor(private readonly febpop: Febpop = febpop('http://example.com/')) {
  }

  onConnect = this.febpop.on<unknown>('connect')
  onToggle = this.febpop.on<'on' | 'off'>('toggle')

  emitChat = this.febpop.emit<{ requestKey: number, text: string }>('chat')
  emitCount = this.febpop.emit<number>('count')
}
```

_Important: You may want to ensure callbacks at the server-side, as such:_

```ts
import { listen } from 'socket.io'

cosnt server = listen(80)

server.on(
  'connect',
  socket => {
    socket.on('event1', (event1, callback) => {
      // Perform server-side logics on `event1`
      // ...
      callback()
    })
  }
)

// Or even more carefully
server.on(
  'connect',
  socket => {
    socket.on('event2', (event2, callback) => {
      try {
        // Perform server-side logics on `event2`
        // ...
      } finally {
        callback()
      }
    })
    socket.on('event3', (event3, callback) => {
      const promise = new Promise(resolve => {
        // Perform server-side logics on `event3`
        // ...
        resolve()
      })
      promise.finally(callback)
    }
  }
)
```

---
_Source: https://npm.io/package/febpop · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
