PondSocket is a fast, minimalist and bidirectional socket framework for NodeJS.

Overview

PondSocket

PondSocket is a fast, minimalist and bidirectional socket framework for NodeJS. Pond allows you to think of each action during a sockets lifetime as a request instead of a huge callback that exists inside the connection event.

Documentation

This is a Node.js module available through the npm registry.

  npm install pondsocket

PondSocket usage depends on the environment in which it is being used.

On the server

When using PondSocket, an endpoint is created. The endpoint is the gateway by which sockets actually connect to the server. Multiple endpoints can be created but every endpoint is independent from the other, ie sockets on one endpoint cannot communicate with sockets on another endpoint.

  import PondSocket from "pondsocket";
  import parse from "url";
  
  const pond = new PondSocket();
 
  const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
       const { query } = parse(req.url || '');     
       const { token } = query;     
       if (!token)         
            return res.reject('No token provided');      
       res.accept({
            assign: {
                token
            }
       });  
  })

While sockets connect through the endpoint, communication between sockets cannot occur on the endpoint level. Sockets have to join a channel to communicate between themselves.

  const channel = endpoint.createChannel(/^channel(.*?)/, (req, res, channel) => {
       const isAdmin = req.clientAssigns.admin;
       if (!isAdmin)       
            return res.reject('You are not an admin');

       res.accept({
           assign: {
               admin: true, 
               joinedDate: new Date()
            }, 
            presence: {
                state: 'online'
            }, 
            channelData: {
                locked: true,
                numberOfUsers: channel.presence.length
            }
        });  
   });   

A user goes through the createChannel function to join a channel. When a user joins a channel, some private information can be assigned to the user. This assign could be viewed as a cookie that is only available serverside. The presence is the current state of the user. When you reassign a new presence information to a user, all other users connected to the same channel are informed of the change. This could be used as user is typing, user is away, etc. The channelData is information that is stored on the channel and accessible from anywhere the channel is available. It can be anything from a boolean to an instance of a class. This data cannot be accessed from another channel as it is private to the channel.

    channel.on('hello', (req, res, channel) => {      
       const users = channel.getPresence();      
       res.assign({
           assign: {
               pingDate: new Date(),
               users: users.length
           }
        }); 

        // res.reject('curse words are not allowed on a child friendly channel') 
        // channel.closeFromChannel(req.client.clientId);
    })

When a message is sent on a channel by a user, an event is triggered. The on function can be used to listen for these events. If the function is specified, it is called when the message is received. You can choose to decline the message being sent or you can allow the message to be sent as usual. You can also do all the normal assigns to the channel, or user. In case there is no on function, the message will be sent without any action being taken.

On the browser

    import PondClientSocket from "pondsocket/client";

    export const socket = new PondClientSocket('/api/socket', {});
    socket.connect();

The browser compatible package can be imported from pondsocket/client. A url string is provided to the class along with other url params, like token.

Multiple classes can be created but it is advised to use a single class throughout the application. You can just create multiple channels and maintain the single socket connection.

    const channelTopic = 'channel:one';
    const options = {
        username: 'eleven-am'
    }

    export const channel = socket.createChannel(channelTopic, options);
    channel.join();

When connected to the channel you can subscribe to the events from the channel.

    const subscriptionPresence = channel.onPresenceUpdate(presence => {
        // handle the presence changes of the channel
    });

    const subscriptionMessage = channel.onMessage((event, data) => {
        // handle the message being received 
    });

    // When done with the channel remember to unsubscribe from these listeners
    subscriptionPresence.unsubscribe();
    subscriptionMessage.unsubscribe();

There are many other features available on the channel object. Since the application is completely typed, suggestions should be provided by your IDE.

    channel.broadcast('hello', {
        name: 'eleven-am',
        message: 'I am the man, man'
    })

    // channel.broadcastFrom broadcasts a message to everyone but the client that emitted the message
    // channel.sendMessage sends a message to clients specified in the function
You might also like...

A node.js module for websocket server and client

Nodejs Websocket A nodejs module for websocket server and client How to use it Install with npm install nodejs-websocket or put all files in a folder

Dec 13, 2022

Standards-compliant WebSocket client and server

faye-websocket This is a general-purpose WebSocket implementation extracted from the Faye project. It provides classes for easily building WebSocket s

Dec 23, 2022

JSON-RPC 2.0 implementation over WebSockets for Node.js and JavaScript/TypeScript

JSON-RPC 2.0 implementation over WebSockets for Node.js and JavaScript/TypeScript

WebSockets for Node.js and JavaScript/TypeScript with JSON RPC 2.0 support on top. About The rpc-websockets library enables developers to easily imple

Dec 21, 2022

simple chat app created with nextjs, express, tailwindcss, and WebSockets

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Sep 10, 2022

A simple web app that people can chat and send images into.

A simple web app that people can chat and send images into.

Typsnd Typsnd. Type, send. It's as simple as that. Typsnd is a simple web app that people can chat and send images into. It is based on Express.JS, No

Nov 10, 2022

Website to display chats and gifts in realtime from your TikTok LIVE stream. Demo project for TikTok-Live-Connector library.

Website to display chats and gifts in realtime from your TikTok LIVE stream. Demo project for TikTok-Live-Connector library.

TikTok-Chat-Reader A chat reader for TikTok LIVE utilizing TikTok-Live-Connector and Socket.IO to forward the data to the client. This demo project us

Dec 31, 2022

WebRTC based peer to peer video calling and messaging web app build with MERN stack.

talkhouse WebRTC based peer to peer video calling and messaging web app build with MERN stack. Demo Libraries used React for frontend Socket.io as sig

Nov 26, 2022

Node.js library to receive live stream chat events like comments and gifts in realtime from TikTok LIVE.

TikTok-Live-Connector A Node.js library to receive live stream events such as comments and gifts in realtime from TikTok LIVE by connecting to TikTok'

Jan 4, 2023

Protobuf RPC for TypeScript and Go with streaming support.

Stream RPC starpc implements Proto3 services (server & client) in both TypeScript and Go. Supports client-to-server streaming RPCs in the web browser,

Dec 14, 2022
Releases(0.1.141)
Owner
Roy Ossai
Roy Ossai
Socket.io-Express - A simple express project to learn how to use socket.io.

Socket.io Express A simple express project to learn how to use socket.io ✨ ?? Simple project This is a small project that has been designed to be usab

Zerio 6 Sep 25, 2022
Lightweight WebSocket lib with socket.io-like event handling, requests, and channels

ws-wrapper Lightweight and isomorphic Web Socket lib with socket.io-like event handling, Promise-based requests, and channels. What? Much like Socket.

Blake Miner 70 Dec 23, 2022
Create a Real-time Chat App using React and Socket.io

React And Socket.io ChatApp MIT Licence MIT License Copyright (c) 2021 Ali Ahmad Permission is hereby granted, free of charge, to any person obtaining

Ali Ahmad 2 Jan 10, 2022
An example about web socket with .net 3.1 and react

Websocket Example This is a real time app example which is using web socket, dot net core and react. Project Case You are a factory manager and you ha

Samet Yazıcı 5 Jan 11, 2022
Full-Stack Instgram Clone using MERN Stack and Socket.io

Instagram MERN Full-Stack Instgram Clone using MERN Stack and Socket.io Visit Now ?? ??️ Tech Stack Frontend: Backend: Realtime Communication: Cloud S

Jigar Sable 326 Dec 27, 2022
🔄 iola: Socket client with REST API

?? iola: Socket client with REST API

Pavel Varentsov 113 Jan 3, 2023
A Develop Tool to Test WebSocket, Socket.IO, Stomp, Bayeux, HTTP, TCP, UDP, WebRTC, DNS API.

A Develop Tool to Test WebSocket, Socket.IO, Stomp, Bayeux, HTTP, TCP, UDP, WebRTC, DNS API.

York Yao 24 Sep 6, 2022
How to build a chat using Lambda + WebSocket + API Gateway? (nodejs)

Description Source code for the lambda function from the screencast How to build a chat using Lambda + WebSocket + API Gateway? (nodejs) The reactjs c

Alex 21 Dec 28, 2022
Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js

ws: a Node.js WebSocket library ws is a simple to use, blazing fast, and thoroughly tested WebSocket client and server implementation. Passes the quit

WebSockets 19.2k Jan 4, 2023
soketi is your simple, fast, and resilient open-source WebSockets server

soketi soketi is your simple, fast, and resilient open-source WebSockets server. ?? Blazing fast speed ⚡ The server is built on top of uWebSockets.js

Soketi 3.2k Jan 4, 2023