Skip to content
/ sockittt Public

pretty good websocket client for the browser & other `WebSocket` compatible runtimes

Notifications You must be signed in to change notification settings

alii/sockittt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sockittt

A sweet little WebSocket client for the browser and other WebSocket-compatible runtimes

Installation

yarn add sockittt

Usage

import { Sockittt } from "sockittt";

const subscriptions = new Set<(event: MessageEvent) => void>();

const ws = new Sockittt("ws://localhost:8080", {
	onMessage: message => {
		for (const subscription of subscriptions) {
			subscription(message);
		}
	},
}).open(); // Call .open() to immediately open the connection, this returns the instance

subscriptions.add(event => {
	console.log("Received", event.data);
});

ws.send("Hello, world!");

Advanced usage

import { Sockittt } from "sockittt";

const ws = new Sockittt("ws://localhost:8080", {
	timeout: 5000,
	maxAttempts: 10,
	onMessage: message => {
		console.log("Received", message.data);
	},
	onOpen: () => {
		console.log("Connected!");
	},
	onClose: () => {
		console.log("Disconnected!");
	},
	onReconnect: () => {
		console.log("Reconnecting...");
	},
	onDidExhaustMaxAttempts: () => {
		console.log("Reconnection attempts exceeded maxAttempts");
	},
	onError: error => {
		console.error("Error", error);
	},
	with: ws => {
		ws.binaryType = "arraybuffer";
	},
}).open();

About

pretty good websocket client for the browser & other `WebSocket` compatible runtimes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published