# gsockets

> Series of generic socket events

Latest version **0.4.1** (published 2015-03-02) · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.1 |
| Published | 2015-03-02 |
| First published | 2014-11-06 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Author | Nick Sheffield |
| Maintainers | nicksheffield |
| Keywords | sockets, socket.io, events |

## Links

- npm: https://www.npmjs.com/package/gsockets
- Repository: https://github.com/nicksheffield/gsockets
- Issues: https://github.com/nicksheffield/gsockets/issues
- npm.io page: https://npm.io/package/gsockets

## Dependencies (2)

- [colors](https://npm.io/package/colors.md) ^1.0.3
- [socket.io](https://npm.io/package/socket.io.md) ~1.2.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.4.1 (latest) — 2015-03-02
- 0.4.0 — 2014-12-11
- 0.3.0 — 2014-11-11
- 0.2.2 — 2014-11-10
- 0.2.1 — 2014-11-10
- 0.2.0 — 2014-11-10
- 0.1.7 — 2014-11-10
- 0.1.6 — 2014-11-09
- 0.1.5 — 2014-11-09
- 0.1.4 — 2014-11-06
- 0.1.3 — 2014-11-06
- 0.1.2 — 2014-11-06
- 0.1.1 — 2014-11-06
- 0.1.0 — 2014-11-06

## README

# Generic Socket Events

A set of generic events for sending and receiving json objects through socket.io.

Allows you to easily announce, broadcast and directly send data to other sockets, and also join/leave rooms through a basic client-side API.

## Setup

### NPM

```
$ npm install gsockets
```

### Server

```javascript
var gsockets = require('gsockets');

// to enable debug logs, add this line
gsockets.config({ log: true});

// express middleware to host the client.js file at /gsockets/client.js
app.use(gsockets.client);
```

_Using existing Socket.IO_

```javascript
io.on('connect', function(socket){
	gsockets.bind(io, socket);
});
```

_Using standalone mode_

```javascript
// choose whatever port number you want to use
gsockets.listen(1234);
```

### Client

```html
<script src="https://cdn.socket.io/socket.io-1.0.6.js"></script>
<script src="/gsockets/client.js"></script>
<script>
	gsockets.connect();

	gsockets.broadcast('user_connected');

	gsockets.on('user_connected', function(){
		console.log('A user has connected');
	});
</script>
```


## Server API

#### `gsockets.client(req, res, next)`

Serve the client.js file at the url `/gsockets/client.js`

 * usage: `app.use(gsockets.client);`



#### `gsockets.config(obj)`

Modify configuration settings of gsockets. The only option currently available is log.

 * usage: `gsockets.config({ log: true});`



#### `gsockets.bind(io, socket)`

Add the gsockets events to your socket.io server



#### `gsockets.listen(port)`

Create a separate instance of socket.io especially to run gsockets.



## Client API



#### `gsockets.connect([ip], [port])`

Create the connection to the socket.io server
 
 * _ip_ (optional) The ip of the server if different from the current page
 * _port_ (optional) The port of the server



#### `gsockets.socket`

The orginal socket.io object.

 * usage: `var socketID = gsockets.socket.id;`



#### `gsockets.on(event, callback)`

Run the callback when an event is emited from the server

 * _event_ The name of the event to listen for
 * _callback_ The function to run when this event is fired



#### `gsockets.onDisconnect(callback)`

Run the callback when a socket disconnects

 * _callback_ The function to run when this event is fired



#### `gsockets.announce(event, [data])`

Emit event to all sockets

 * _event_ The name of the event that will be sent to another client
 * _data_ (optional) Data object



#### `gsockets.bounce(event, [data])`

Emit event to this socket

 * _event_ The name of the event that will be sent to another client
 * _data_ (optional) Data object



#### `gsockets.send(socketID, event, [data])`

Emit event to specific socket

 * _socketID_ The socket id to send this data to
 * _event_ The name of the event that will be sent to another client
 * _data_ (optional) Data object



#### `gsockets.broadcast(event, [data])`

Emit event to every socket except this one

 * _event_ The name of the event that will be sent to another client
 * _data_ (optional) Data object



#### `gsockets.roomAnnounce(roomName, event, [data])`

Emit event to room

 * _roomName_ The name of the room to emit to
 * _event_ The name of the event that will be sent to another client
 * _data_ (optional) Data object



#### `gsockets.roomBroadcast(roomName, event, [data])`

Emit event to everyone in room except you

 * _roomName_ The name of the room to emit to
 * _event_ The name of the event that will be sent to another client
 * _data_ (optional) Data object



#### `gsockets.join(roomName, event)`

Join room and emit an event to this socket

 * _roomName_ The name of the room to join
 * _event_ The name of the event that will be sent to another client



#### `gsockets.leave(roomName, event)`

Leave room and emit an event to this socket

 * _roomName_ The name of the room to leave
 * _event_ The name of the event that will be sent to another client

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