# konnectjs

> An abstraction of node-connections structure, which designed especially for keep-in-connection scene. can seamlessly switch protocols with: tcp udp ws polling sse http3 kcp, etc...

Latest version **0.2.1** (published 2023-02-08) · ISC license · 0 weekly downloads

## Install

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

## 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.1 |
| Published | 2023-02-08 |
| First published | 2023-02-02 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 39.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | labbbirder |
| Maintainers | bbbirder |
| Keywords | udp, tcp, ws, websocket, sse, polling, socket, kcp |

## Links

- npm: https://www.npmjs.com/package/konnectjs
- Repository: https://github.com/labbbirder/KonnectJS
- Homepage: https://github.com/labbbirder/KonnectJS#readme
- Issues: https://github.com/labbbirder/KonnectJS/issues
- npm.io page: https://npm.io/package/konnectjs

## Dependencies (6)

- [bl](https://npm.io/package/bl.md) ^6.0.0
- [chalk](https://npm.io/package/chalk.md) ^4.1.2
- [events](https://npm.io/package/events.md) ^3.3.0
- [ts-log](https://npm.io/package/ts-log.md) ^2.2.5
- [koa-compose](https://npm.io/package/koa-compose.md) ^4.1.0
- [koa-convert](https://npm.io/package/koa-convert.md) ^2.0.0

## 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.1 (latest) — 2023-02-08
- 0.2.0 — 2023-02-07
- 0.1.5 — 2023-02-03
- 0.1.4 — 2023-02-03
- 0.1.3 — 2023-02-02
- 0.1.2 — 2023-02-02
- 0.1.1 — 2023-02-02
- 0.1.0 — 2023-02-02

## README

![logo](https://github.com/labbbirder/KonnectJS/blob/main/docs/logo.png)

An extremely flexible abstraction of node-connections structure, which designed for keep-in-connection using, can be fit with any type of network protocol

this work is still in progress.

<!-- vscode-markdown-toc -->
- [Installation](#installation)
- [Basic Usage](#basic-usage)
  - [Start A WebSocket Server](#start-a-websocket-server)
  - [Start A Tcp Server](#start-a-tcp-server)
  - [Hybrid Server](#hybrid-server)
- [Documentation](#documentation)

<!-- vscode-markdown-toc-config
	numbering=true
	autoSave=true
	/vscode-markdown-toc-config -->
<!-- /vscode-markdown-toc -->


## Installation
or install from npmjs:

```sh
> npm i -S konnectjs
```
in editor:
```typescript
import { Knode,Konnection } from 'KonnectJS'
```
## Basic Usage
### Start A WebSocket Server
the code below illustrates how a websocket server is created:
```typescript
import { Knode,Konnection } from 'konnectjs'
import { WebSocketBroker } from 'konnect-ws'

let node = new Knode()
.setBroker(new WebSocketBroker({ port:3000, isPublic:true })) // Immediately listen on 3000, and communicate with websocket
.use(()=>ctx=>{
    console.log("websocket message", ctx.eventType, ctx.dataIn)
})
```
### Start A Tcp Server
the code below illustrates how a tcp server is created:
```typescript
import { Knode,Konnection } from 'konnectjs'
import { TcpBroker } from 'konnect-tcp'

let node = new Knode()
.setBroker(new TcpBroker({ port:3000, isPublic:true })) // now it's upon TCP
.use(()=>ctx=>{
    console.log("tcp data", ctx.eventType, ctx.dataIn)
})
```
### Hybrid Server
the code below illustrates how a server accepting either WebScoket or TCP connections!
```typescript
import { Knode,Konnection } from 'konnectjs'
import { TcpBroker } from 'konnect-tcp'
import { WebSocketBroker } from 'konnect-ws'


let wsNode = new Knode() // websocket
.setBroker(new WebSocketBroker({ port:3000, isPublic:true }))
.use(()=>(ctx,next)=>{
  console.log("raw WebSocket",ctx.eventType)
  next()
})
.to(()=>endNode) // continue with endNode


let tcpNode = new Knode() // tcp
.setBroker(new TcpBroker({ port:3000, isPublic:true }))
.use(()=>(ctx,next)=>{
  console.log("raw TCP",ctx.eventType)
  next()
})
.to(()=>endNode) // continue with endNode


let endNode = new Knode()
.use(()=>(ctx)=>{
  // both TCP and WS events are redirected here
  console.log("application event",ctx.eventType) 
  ctx.send(ctx.dataIn) // send back
})

```
## Documentation
See full document [here](https://github.com/labbbirder/KonnectJS/blob/main/README.md)

there are alse samples [here](https://github.com/labbbirder/KonnectJS/blob/main/samples):
* chat - a simple chat room with websock server and client
* switch_protocols - a chat room, but can swtich with varied protocols
* chat2 - a chat room, but with packet split, autoreconnect, heartbeat strategies
* simple - a simple transmitter between knodes

see step-by-step tutorial [here](https://github.com/labbbirder/KonnectJS/blob/main/packages/tutorial/README.md)

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