All Projects → mat-sz → typesocket

mat-sz / typesocket

Licence: BSD-3-Clause-Clear License
🌐 TypeScript WebSockets library.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to typesocket

Websockets
Library for building WebSocket servers and clients in Python
Stars: ✭ 3,724 (+15416.67%)
Mutual labels:  websockets, websocket-library
simple-websocket-server
A simple WebSocket server
Stars: ✭ 26 (+8.33%)
Mutual labels:  websockets, websocket-library
mojo.js
🦄 The Mojolicious real-time web framework for Node.js
Stars: ✭ 145 (+504.17%)
Mutual labels:  websockets
stan
🔨 Collection of front-end engineering tools
Stars: ✭ 19 (-20.83%)
Mutual labels:  typescript-library
gochess
Online real time chess web server using websockets
Stars: ✭ 32 (+33.33%)
Mutual labels:  websockets
tzientist
Scientist-like library for Node.js in TypeScript
Stars: ✭ 37 (+54.17%)
Mutual labels:  typescript-library
node-red-contrib-blynk-websockets
No description or website provided.
Stars: ✭ 35 (+45.83%)
Mutual labels:  websockets
play2-sockjs
A SockJS server implementation for Play Framework.
Stars: ✭ 60 (+150%)
Mutual labels:  websockets
rustic
rustic is a TypeScript library providing emulation of Rust's Option and Result types (and some useful wrappers for common js functions as well!)
Stars: ✭ 71 (+195.83%)
Mutual labels:  typescript-library
karkas
A tiny template engine based on TypeScript
Stars: ✭ 14 (-41.67%)
Mutual labels:  typescript-library
WebsocketBundle
Provides websocket services for Symfony2, including an in-built server, multiplexing, and semantic configuration. Uses Wrench.
Stars: ✭ 50 (+108.33%)
Mutual labels:  websockets
WebSocketPipe
System.IO.Pipelines API adapter for System.Net.WebSockets
Stars: ✭ 17 (-29.17%)
Mutual labels:  websockets
Lecture2Gether
A tool for synchronized online streaming
Stars: ✭ 52 (+116.67%)
Mutual labels:  websockets
voxeling
Creative mode WebGL voxel game. Runs in Chrome, with multiplayer functionality and very few dependencies
Stars: ✭ 24 (+0%)
Mutual labels:  websockets
django-rest-live
Subscribe to updates from Django REST Framework over Websockets.
Stars: ✭ 48 (+100%)
Mutual labels:  websockets
amazon-ivs-simple-chat-web-demo
⚠️ IMPORTANT ⚠️ This repository is no longer actively maintained and will be archived at the end of 2022. A basic live chat implementation built with WebSockets, that can be used in conjunction with Amazon IVS to build compelling customer experiences for live video streams with chat use cases.
Stars: ✭ 53 (+120.83%)
Mutual labels:  websockets
safe-touch
⛓ Runtime optional chaining for JS
Stars: ✭ 71 (+195.83%)
Mutual labels:  typescript-library
real-time-todo
A real time todo list using websockets
Stars: ✭ 22 (-8.33%)
Mutual labels:  websockets
dataviewer-example
📊 Usage examples of dataviewer - https://github.com/jasrodis/dataviewer
Stars: ✭ 15 (-37.5%)
Mutual labels:  websockets
apollo-chat-graphql-server
Apollo Chat is a Chat Service build on GraphQL Apollo with Subscriptions
Stars: ✭ 13 (-45.83%)
Mutual labels:  websockets

TypeSocket

TypeScript WebSocket wrapper with disconnection handling and JSON parsing.

workflow npm npm NPM

Are you a React.js user? You might be interested in the useTypeSocket React hook.

Used in filedrop-web and whiteboard-web.

API is mostly stable. It may change in the future, but the changes shouldn't be breaking.

Installation

TypeSocket is available on npm, you can install it with either npm or yarn:

npm install typesocket
# or:
yarn install typesocket

Example usage

const socket = new TypeSocket<MessageModel>(url, {
  maxRetries: 10,
});

socket.on('connected', () => {
  console.log('connected!');

  socket.send({
    type: 'ping',
  });
});

socket.on('message', (message: MessageModel) => {
  console.log(message.type);
});

socket.on('disconnected', () => {
  console.log('disconnected');
});

socket.connect();

With Redux:

I use TypeSocket with Redux and Redux-Saga like this:

export const socketMiddleware = (url: string) => {
  return (store: MiddlewareAPI<any, any>) => {
    const socket = new TypeSocket<MessageModel>(url);

    socket.on('connected', () =>
      store.dispatch({ type: ActionType.WS_CONNECTED })
    );
    socket.on('disconnected', () =>
      store.dispatch({ type: ActionType.WS_DISCONNECTED })
    );
    socket.on('message', message =>
      store.dispatch({ type: ActionType.WS_MESSAGE, value: message })
    );
    socket.connect();

    return (next: (action: any) => void) => (action: any) => {
      if (
        action.type &&
        action.type === ActionType.WS_SEND_MESSAGE &&
        socket.readyState === 1
      ) {
        socket.send(action.value);
      }

      return next(action);
    };
  };
};

Events

You can attach event listeners to an instance of TypeSocket with .on:

socket.on('message', message => {
  console.log(message);
});

connected

Emitted when a connection gets established.

disconnected

Emitted when the socket is disconnected.

permanentlyDisconnected

Emitted when the socket is permanently disconnected, for example:

  • Server gracefully closes the connection.
  • Client gracefully closes the connection.
  • disconnect is called.
  • Retry amount has been exceeded.

message

Emitted when a valid message is received.

The only argument contains an object of type T with a deserialized message.

invalidMessage

Emitted when an invalid message is received.

The only argument contains an object of type string | ArrayBuffer | Blob | ArrayBufferView with a raw message.

rawMessage

Emitted when any message is received.

The only argument contains an object of type string | ArrayBuffer | Blob | ArrayBufferView with a raw message.

API

on(eventType: 'message', listener: (message: T) => void);
on(eventType: 'rawMessage' | 'invalidMessage', listener: (message: string | ArrayBuffer | Blob | ArrayBufferView) => void);
on(eventType: 'connected' | 'disconnected' | 'permanentlyDisconnected', listener: () => void);

off(eventType: 'message', listener: (message: T) => void);
off(eventType: 'rawMessage' | 'invalidMessage', listener: (message: string | ArrayBuffer | Blob | ArrayBufferView) => void);
off(eventType: 'connected' | 'disconnected' | 'permanentlyDisconnected', listener: () => void);

constructor(private url: string, options?: TypeSocketOptions);
connect();
disconnect();
send(data: T);
sendRaw(data: string | ArrayBuffer | Blob | ArrayBufferView);
get readyState();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].