# @gden/socket

> A socket service package for handling WebSocket connections.

Latest version **1.0.1** (published 2023-09-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @gden/socket
pnpm add @gden/socket
yarn add @gden/socket
bun add @gden/socket
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2023-09-14 |
| First published | 2023-09-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 9.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | George Denning |
| Maintainers | gden |
| Keywords | socket, websocket, socket.io, socket.io-client |

## Links

- npm: https://www.npmjs.com/package/@gden/socket
- Repository: https://github.com/georgedenning/socket
- Homepage: https://github.com/georgedenning/socket#readme
- Issues: https://github.com/georgedenning/socket/issues
- npm.io page: https://npm.io/package/@gden/socket

## Dependencies (1)

- [socket.io-client](https://npm.io/package/socket.io-client.md) 4.7.2

## 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

- 1.0.1 (latest) — 2023-09-14
- 1.0.0 — 2023-09-14

## README

# @gden/socket

![NPM License](https://img.shields.io/npm/l/@gden/socket?label=)

<!-- ![NPM Downloads](https://img.shields.io/npm/dw/@gden/socket?label=) -->

`@gden/socket` is a JavaScript library that provides a versatile and configurable WebSocket client service for handling WebSocket connections in your applications.

## Installation

```shell
npm install @gden/socket
```

### Importing the Package:

```js
import SocketService from '@gden/socket';
```

## Usage

Once you've imported the `SocketService` class, you can create WebSocket connections and manage events.

### Creating a Socket Connection

```js
import SocketService from '@gden/socket';

// Initialize a new WebSocket connection with optional configuration options
const socket = new SocketService(options);
```

### Listening for WebSocket Events

You can listen for WebSocket events, such as incoming messages, using the on method:

```js
socket.on('message', msg => {
    // Handle incoming message
    console.log('Received message:', msg);
});
```

### Sending Messages

You can send messages to the server using the emit method:

```js
const sendMessage = msg => {
    // Send a message to the server
    socket.emit('message', msg);
};

sendMessage('Hello, server!');
```

### Disconnecting from the WebSocket

To gracefully close the WebSocket connection, you can use the disconnect method:

```js
socket.disconnect();
```

### Demo usage:

```js
import SocketService from '@gden/socket';

const socket = new SocketService(options);

let messages = [];

socket.on('messages', msgs => {
    messages = msgs;
});

socket.on('message', msg => {
    messages.push(msg);
});

const sendMessage = msg => {
    socket.emit('message', msg);
};

sendMessage('test');
```

## Configuration Options

When creating a `SocketService` instance, you can pass in various configuration options to customize its behavior. Here are the available options:

-   `url` (string): The WebSocket server URL.
-   `autoConnect` (boolean): Automatically connect on instantiation.
-   `reconnect` (boolean): Enable reconnection on disconnect.
-   `reconnectionAttempts` (number): Number of reconnection attempts.
-   `reconnectionDelay` (number): Initial delay before reconnection (in milliseconds).
-   `reconnectionDelayMax` (number): Maximum delay between reconnection attempts (in milliseconds).
-   `query` (object): Custom query parameters to send during the WebSocket handshake.
-   `transports` (array): Available transport methods.
-   `timeout` (number): Connection timeout (in milliseconds).
-   `events` (object): Custom event listeners.
-   `logger` (object): Logger for connection events and errors.
-   `middleware` (array): Middleware functions to apply to outgoing messages.
-   `socketOptions` (object): Additional socket.io-client options.

## License

This project is licensed under the terms of the [MIT license](/LICENSE).

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