Giter Club home page Giter Club logo

websocket-provider's Introduction

Websocket Provider

This code is intended to be a basic abstraction of the base websocket API which you can then build your websocket around. This code is written with the intent that it be taken and changed as needed to fit the needs of your individual projects. Hence why it does not have an npm repository set up.

WebSocket Server

Creating a new WebSocket server instance is as easy as calling new WebsocketProvider(port) you can then set parameters and event listeners.

const init = async (): void => {
    const socket: WebsocketProvider = new WebsocketProvider(8080);

    socket.on("message",(message: string) => {
        console.log(message);
    });

    if(!(await socket.connect())) return console.error('Unable to start the connection!');

    socket.send('hello world');
}
init();

Event Listeners

There are four events that you can listen for; connect, message, close, and error.

NOTE: Event listeners must be defined before you start the WebSocket connection

connect

socket.on('connect',(connection: WebSocket) => {
    
});

message

socket.on('message',(message: string) => {
    
});

close

socket.on('close',() => {
    
});

error

socket.on('error',() => {
    
});

Methods

.on()

Listen for an event to fire.

.connect()

Open the WebSocket connection.

.send()

Send a message over the connection.

  • param message - The message to be sent.
  • throws - An error if you try to send a message when the connection is closed.

.ping()

Ping the connection.

  • throws - An error if you try to ping the connection while it's closed.

.close()

Close the connection.

WebSocket Subscriber

Creating a subscriber is as easy as making a provider.

const init = async (): void => {
    const uri = new URIBuilder()
                    .setProtocol(StandardProtocol.WS)
                    .setHostname("localhost")
                    .setPort(8080)
                    .build();

    const socket = new WebsocketSubscriber(uri);
    socket.reconnect(2);

    socket.on("message",(message: string) => {
        console.log(message);
    });
    
    if(!(await socket.connect())) throw new Error('Unable to start the connection!');
}
init();

Event Listeners

There are five events that you can listen for; connect, message, close, error, and close.

NOTE: Event listeners must be defined before you start the WebSocket connection

connect

socket.on('connect',(connection: WebSocket) => {
    
});

message

socket.on('message',(message: string) => {
    
});

close

socket.on('close',() => {
    
});

error

socket.on('error',() => {
    
});

reconnect

socket.on('reconnect',() => {
    
});

Methods

.on()

Listen for an event to fire.

.connect()

Open the WebSocket connection.

.send()

Send a message over the connection.

  • param message - The message to be sent.
  • throws - An error if you try to send a message when the connection is closed.

.ping()

Ping the connection.

  • throws - An error if you try to ping the connection while it's closed.

.close()

Close the connection.

.kill()

Kill the connection. The only difference between this and .close() is that this will not allow the connection to be restarted; even if .reconnect() has been set.

.reconnect()

Set this Websocket to attempt to reconnect after a delay (in seconds). Default's to disabled

  • param delay - Default null - The amount of time to wait (in seconds) before attempting to reconnect. If set to null, the connection will not attempt to reconnect on close.

websocket-provider's People

Contributors

nathan-i-martin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.