Blinchik
A tiny websocket wrapper that made using Kefir.js. It returns a Kefir stream instance.
Etymology
Blinchik — diminutive form of Blin. Blini frequently made using Kefir.
Getting started
- Installation.
npm i -S blinchik
- Look into examples.
You can also scale your code using Blinchik.
That's the client side:
import Blinchik from 'blinchik'
const b = new Blinchik('ws://127.0.0.1:8080')
const stream = b.onMsg({ shouldParseJSON: true })
const chatStream = stream
.filter(({ data }) => data.type === 'CHAT/INCOMING_MESSAGE')
const notificationStream = stream
.filter(({ data }) => data.type === 'NOTIFICATION/PAYMENT')
chatStream
.log('chat stream')
.map(({ data }) => {
// your logic goes here
})
notificationStream
.log('notification stream')
.map(({ data }) => {
// your logic goes here.
// e.g., you can show a popup with the notification
})
And that's the server side:
import Blinchik from 'blinchik'
const b = new Blinchik({ port: 8080 })
const connectionsStream = b.onConn()
connectionsStream
.onValue(({ conn }) => {
// for demonstration purposes,
// on each new connection we send
// two messages to the client.
// you can implement any custom logic instead.
b.send({
type: 'CHAT/INCOMING_MESSAGE',
text: 'LMAO',
}, conn)
b.send({
type: 'NOTIFICATION/PAYMENT',
severity: 'error',
text: 'Could not process scheduled payment. Please, review your billing settings.',
}, conn)
})
Using Blinchik, you can use all Kefir stream methods. See Kefir docs for more information.
- You're ready to go ;)
API
Blinchik constructor parameters
new Blinchik(ws, settings)
ws
must be one of:
- undefined (for creating mock instance)
- Blinchik instance (for using mock instance)
- String (node & browser client only):
wss://ws.example.com/connections
. - Object (server only):
WebSocket.Server
options fromws
library - WebSocket or WebSocket.Server from ws library
- Standard WebSocket object
settings
is an optional object with:
disableReconnect
(node & browser client only, Boolean, default: false): disables automatic failover behaviour, which will try to reconnect to a broken connection eachreconnectInterval
ms.reconnectInterval
(node & browser client only, Number, default: 2000): failover reconnection interval in milliseconds.mock
mock Blinchik instance or array mock Blinchik instances
Blinchik instance properties
ws
eighter is one of:
- WebSocket or WebSocket.Server from ws library
- Standard WebSocket object
Blinchik instance will use this connection.
Blinchik methods for both server and clients
onError(callback)
: sets error handler for current instance. Callback should accept an error. Error type depends on mode.onClose(callback)
: sets connection close handler for current instance. Callback should accept an error. Error type depends on mode.onMessage(options)
: returns Kefir stream of connection messages.options
is an optional object withshouldParseJSON
boolean flag, that enables automatic data parsing.
onMsg
: alias foronMessage
send(msg, ws)
: sends given message using current instance connection or givenws
connection, if passed. This method handles connection state checks and automatic data stringify if given message is an object (which is not a valid WS message).
Blinchick client methods
onOpen(callback)
: sets a handler which executed on successful connection.onPing(callback)
(only node client): sets a handler for pings. For details, seews
library docs.
Blinchik server methods
onConnection()
: returns Kefir stream of new connections.onConn
: alias for onConnectiononHeaders()
: returns Kefir stream which allows set custom headers. This is done by mutatingheaders
property of the value.
Blinchik mock methods
connect()
: create mock connection
helpers
setCookie(cookie)(params)
: helper for onHeaders stream.
cookie
is object with:
- value?: string;
- expires?: string | Date;
- name?: string;
- isReplace?: boolean;
- ttl?: number;
- getValue?: () => string;
- getExpires?: () => string | Date;
params
is object with:
- req: Request Object;
- headers: Array;