@elderapo/typed-socket.io-client
v1.0.5
Published
[![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://
Downloads
6
Readme
@elderapo/typed-socket.io
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);