@elderapo/typed-socket.io-client

[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![Greenkeeper badge](https://badges.greenkeeper.io/elderapo/typed-socket.io.svg)](https://greenkeeper.io/) [![CircleCI](https://

Stats

StarsIssuesVersionUpdatedCreatedSize
@elderapo/typed-socket.io-client
1.0.53 years ago3 years agoMinified + gzip package size for @elderapo/typed-socket.io-client in KB

Readme

@elderapo/typed-socket.io

styled with prettier Greenkeeper badge CircleCI Dev Dependencies

Strongly typed websockets library (internally uses SocketIO).

How to install

yarn add @elderapo/typed-socket.io-server @elderapo/typed-socket.io-client
# or
npm install @elderapo/typed-socket.io-server @elderapo/typed-socket.io-client

Example

import {
  ClientSocket,
  ClientSocketStateEvent,
  ClientSocketStateEvents
} from "@elderapo/typed-socket.io-client";
import {
  Server,
  ServerSocketStateEvent,
  ServerSocketStateEvents
} from "@elderapo/typed-socket.io-server";

export enum ClientToServerEvent {
  HelloServer
}

export interface ClientToServerEvents extends ServerSocketStateEvents {
  [ClientToServerEvent.HelloServer]: string;
}

export enum ServerToClientEvent {
  HelloClient
}

export interface ServerToClientEvents extends ClientSocketStateEvents {
  [ServerToClientEvent.HelloClient]: number;
}

const typedIOServer = new Server<ClientToServerEvents, ServerToClientEvents>(
  8000
);

typedIOServer.onConnection(socket => {
  console.log("New connection:", socket.id);

  socket.emit(ServerToClientEvent.HelloClient, 123);

  socket.on(ClientToServerEvent.HelloServer, payload => {
    console.log("Received hello server:", payload);
  });

  typedIOServer.emitToEveryone(ServerToClientEvent.HelloClient, 666);

  socket.on(ServerSocketStateEvent.Disconnect, reason => {
    console.log("ServerSocketStateEvent.Disconnect", reason);
  });
});

const typedIOClient = ClientSocket.connect<
  ClientToServerEvents,
  ServerToClientEvents
>("http://localhost:8000");

typedIOClient.on(ServerToClientEvent.HelloClient, payload => {
  console.log("ServerToClientEvent.HelloClient", payload);
});

typedIOClient.emit(ClientToServerEvent.HelloServer, "HELLOOOO!!");

typedIOClient.on(ClientSocketStateEvent.Connect, () => {
  console.log("ClientStateEvent.Connect");
});

typedIOClient.on(ClientSocketStateEvent.Disconnect, () => {
  console.log("ClientStateEvent.Disconnect");
});

setTimeout(() => {
  typedIOClient.disconnect();
}, 500);

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.