0.1.212 • Published 5 months ago

@eleven-am/pondsocket v0.1.212

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
5 months ago

PondSocket

PondSocket is a high-performance, minimalist, and bidirectional socket framework designed for Node.js. It provides a seamless way to handle real-time communication between server and client applications, making it an ideal choice for building WebSocket-based projects.

Installation

To integrate PondSocket into your Node.js project, simply install it via npm:

npm install @eleven-am/pondsocket

Overview

PondSocket simplifies the complexity of handling WebSocket connections by abstracting the communication process into individual requests rather than dealing with intricate callbacks within the connection event. It offers a lightweight yet powerful solution for managing bidirectional communication channels, enabling real-time updates and collaboration between server and client components.

Key Features

  • Simple and Efficient API: PondSocket offers an easy-to-use API, making WebSocket communication straightforward and hassle-free.
  • Organized Channels: Channels provide a structured approach for grouping users and facilitating efficient communication.
  • Assigns: PondSocket allows the storage of private information for users and channels, enhancing data security.
  • Presence: The presence feature keeps track of users' current states and notifies other users about any changes.
  • Broadcasting: PondSocket enables broadcasting messages to all users or specific groups within a channel, facilitating real-time updates.
  • Type Safety: The codebase is thoroughly typed with TypeScript, providing a seamless development experience with improved IDE suggestions.
  • Middleware Support: Extensible middleware system for request processing and validation.
  • Distributed Support: Built-in support for distributed deployment with state synchronization.

Server-side Usage

When setting up the server, PondSocket allows you to create multiple endpoints, each serving as a gateway for sockets to connect and communicate. Each endpoint operates independently, ensuring that sockets from one endpoint cannot interact with sockets from another. This isolation enhances security and simplifies resource management.

import PondSocket from "@eleven-am/pondsocket";

const pond = new PondSocket();

// Create an endpoint for handling socket connections
const endpoint = pond.createEndpoint('/api/socket', (ctx, next) => {
    // Handle socket connection and authentication
    const token = ctx.request.query.token;
    
    if (isValidToken(token)) {
        const role = getRoleFromToken(token);
        ctx.accept({ role }); // Assign the user's role to the socket
    } else {
        ctx.reject('Invalid token', 401);
    }
});

// Create a channel with path parameters
const channel = endpoint.createChannel('/channel/:id', (ctx, next) => {
    const { role } = ctx.user.assigns;
    const { username } = ctx.joinParams;
    const { id } = ctx.event.params;

    if (role === 'admin') {
        ctx.accept({ username })
            .trackPresence({
                username,
                role,
                status: 'online',
                onlineSince: Date.now(),
            });
    } else {
        ctx.decline('Insufficient permissions', 403);
    }
});

// Handle channel events
channel.onEvent('message', (ctx, next) => {
    const { text } = ctx.event.payload;
    
    // Broadcast to all users in the channel
    ctx.broadcast('message', { text });
    
    // Or broadcast to specific users
    ctx.broadcastTo(['user1', 'user2'], 'message', { text });
    
    // Or broadcast to all except sender
    ctx.broadcastFrom('message', { text });
});

// Start the server
pond.listen(3000);

Client-side Usage

On the client-side, PondSocket provides the PondClient class to establish connections with the server. Clients can easily initiate connections, join channels, and participate in real-time interactions.

import PondClient from "@eleven-am/pondsocket-client";

// Create a new client instance
const socket = new PondClient('ws://your-server/api/socket', {
    token: 'your-auth-token'
});

// Connect to the server
socket.connect();

// Handle connection state changes
socket.onConnectionChange((connected) => {
    if (connected) {
        console.log('Connected to the server');
    } else {
        console.log('Disconnected from the server');
    }
});

// Create and join a channel
const channel = socket.createChannel('/channel/123', {
    username: 'user123'
});

// Join the channel
channel.join();

// Handle channel events
const subscription = channel.onMessage((event, message) => {
    console.log(`Received message: ${message.text}`);
});

// Send a message
channel.broadcast('message', { text: 'Hello, PondSocket!' });

// Leave the channel
channel.leave();

// Unsubscribe from events
subscription();

Advanced Features

Presence Management

// Server-side
channel.onEvent('presence', (ctx, next) => {
    ctx.trackPresence({
        username: ctx.user.assigns.username,
        status: 'online',
        lastSeen: Date.now()
    });
});

// Client-side
channel.onPresence((presences) => {
    console.log('Current users:', presences);
});

User Assigns

// Server-side
channel.onEvent('update-profile', (ctx, next) => {
    ctx.assign({
        ...ctx.user.assigns,
        profile: ctx.event.payload
    });
});

// Client-side
channel.onAssigns((assigns) => {
    console.log('User data updated:', assigns);
});

Error Handling

// Server-side
channel.onEvent('message', (ctx, next) => {
    try {
        // Your logic here
        ctx.accept();
    } catch (error) {
        ctx.decline(error.message, 400);
    }
});

// Client-side
channel.onError((error) => {
    console.error('Channel error:', error);
});

Distributed Deployment

PondSocket supports distributed deployment through its distributed backend system. This allows you to scale your application across multiple nodes while maintaining state synchronization.

import PondSocket from "@eleven-am/pondsocket";
import { RedisBackend } from "@eleven-am/pondsocket";

const pond = new PondSocket({
    backend: new RedisBackend({
        host: 'localhost',
        port: 6379
    })
});

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the GPL-3.0 License - see the LICENSE file for details.

0.1.205

5 months ago

0.1.208

5 months ago

0.1.207

5 months ago

0.1.202

5 months ago

0.1.201

5 months ago

0.1.204

5 months ago

0.1.203

5 months ago

0.1.200

6 months ago

0.1.209

5 months ago

0.1.212

5 months ago

0.1.211

5 months ago

0.1.210

5 months ago

0.1.190

12 months ago

0.1.192

9 months ago

0.1.191

9 months ago

0.1.198

6 months ago

0.1.197

6 months ago

0.1.199

6 months ago

0.1.194

8 months ago

0.1.193

8 months ago

0.1.196

6 months ago

0.1.195

6 months ago

0.1.189

12 months ago

0.1.187

1 year ago

0.1.188

1 year ago

0.1.186

1 year ago

0.1.158

1 year ago

0.1.157

1 year ago

0.1.169

1 year ago

0.1.168

1 year ago

0.1.165

1 year ago

0.1.164

1 year ago

0.1.167

1 year ago

0.1.166

1 year ago

0.1.161

1 year ago

0.1.160

1 year ago

0.1.163

1 year ago

0.1.162

1 year ago

0.1.170

1 year ago

0.1.179

1 year ago

0.1.176

1 year ago

0.1.175

1 year ago

0.1.178

1 year ago

0.1.177

1 year ago

0.1.172

1 year ago

0.1.171

1 year ago

0.1.174

1 year ago

0.1.173

1 year ago

0.1.181

1 year ago

0.1.180

1 year ago

0.1.183

1 year ago

0.1.182

1 year ago

0.1.185

1 year ago

0.1.184

1 year ago

0.1.155

2 years ago

0.1.154

2 years ago

0.1.153

2 years ago

0.1.152

2 years ago

0.1.151

2 years ago

0.1.149

2 years ago

0.1.148

2 years ago

0.1.150

2 years ago

0.1.147

2 years ago

0.1.146

2 years ago

0.1.143

2 years ago

0.1.142

2 years ago

0.1.145

2 years ago

0.1.144

2 years ago

0.1.139

2 years ago

0.1.136

2 years ago

0.1.138

2 years ago

0.1.137

2 years ago

0.1.141

2 years ago

0.1.135

2 years ago

0.1.134

2 years ago

0.1.129

2 years ago

0.1.132

2 years ago

0.1.131

2 years ago

0.1.133

2 years ago

0.1.130

2 years ago

0.1.128

2 years ago

0.1.127

2 years ago

0.1.125

2 years ago

0.1.124

2 years ago

0.1.126

2 years ago

0.1.123

2 years ago

0.1.122

2 years ago

0.1.118

2 years ago

0.1.119

2 years ago

0.1.114

2 years ago

0.1.113

2 years ago

0.1.116

2 years ago

0.1.115

2 years ago

0.1.121

2 years ago

0.1.120

2 years ago

0.1.110

2 years ago

0.1.112

2 years ago

0.1.111

2 years ago

0.1.107

2 years ago

0.1.106

2 years ago

0.1.109

2 years ago

0.1.108

2 years ago

0.1.103

2 years ago

0.1.105

2 years ago

0.1.104

2 years ago

0.1.96

2 years ago

0.1.97

2 years ago

0.1.98

2 years ago

0.1.99

2 years ago

0.1.94

2 years ago

0.1.95

2 years ago

0.1.102

2 years ago

0.1.101

2 years ago

0.1.100

2 years ago

0.1.90

2 years ago

0.1.91

2 years ago

0.1.92

2 years ago

0.1.93

2 years ago

0.1.85

2 years ago

0.1.86

2 years ago

0.1.87

2 years ago

0.1.88

2 years ago

0.1.89

2 years ago

0.1.82

2 years ago

0.1.83

2 years ago

0.1.84

2 years ago

0.1.81

2 years ago

0.1.74

2 years ago

0.1.75

2 years ago

0.1.76

2 years ago

0.1.77

2 years ago

0.1.78

2 years ago

0.1.79

2 years ago

0.1.73

2 years ago

0.1.72

2 years ago

0.1.71

2 years ago

0.1.70

2 years ago

0.1.65

2 years ago

0.1.66

2 years ago

0.1.67

2 years ago

0.1.68

2 years ago

0.1.69

2 years ago

0.1.52

3 years ago

0.1.53

3 years ago

0.1.54

3 years ago

0.1.55

3 years ago

0.1.56

3 years ago

0.1.12

3 years ago

0.1.57

3 years ago

0.1.13

3 years ago

0.1.58

3 years ago

0.1.14

3 years ago

0.1.59

3 years ago

0.1.15

3 years ago

0.1.50

3 years ago

0.1.51

3 years ago

0.1.49

3 years ago

0.1.41

3 years ago

0.1.42

3 years ago

0.1.43

3 years ago

0.1.45

3 years ago

0.1.46

3 years ago

0.1.47

3 years ago

0.1.48

3 years ago

0.1.40

3 years ago

0.1.38

3 years ago

0.1.39

3 years ago

0.1.30

3 years ago

0.1.31

3 years ago

0.1.32

3 years ago

0.1.33

3 years ago

0.1.34

3 years ago

0.1.35

3 years ago

0.1.36

3 years ago

0.1.37

3 years ago

0.1.27

3 years ago

0.1.28

3 years ago

0.1.29

3 years ago

0.1.63

2 years ago

0.1.64

2 years ago

0.1.20

3 years ago

0.1.21

3 years ago

0.1.22

3 years ago

0.1.23

3 years ago

0.1.24

3 years ago

0.1.25

3 years ago

0.1.26

3 years ago

0.1.60

2 years ago

0.1.61

2 years ago

0.1.62

2 years ago

0.1.16

3 years ago

0.1.17

3 years ago

0.1.18

3 years ago

0.1.19

3 years ago

0.1.11

3 years ago

0.1.10

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago