Skip to content

0liveiraVictor/websockets-communication

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

websockets

Websockets Communication

A simple example of websocket communication between a client and a web server.


license

Content Table

About Websocket Connections

A websocket connection is a bidirectional and real-time communication technology that enables efficient data exchange between a client and a server. Unlike the traditional client-server communication model based on requests and responses (HTTP), websocket establishes a persistent connection that allows for asynchronous real-time data transmission.

The key features of a websocket connection are as follows:

  • Connection establishment:
    The process of establishing a websocket connection involves an initial handshake. The client sends an HTTP request to the server, including a special header called "Upgrade" with the value "websocket," indicating the desire to establish a websocket connection. If the server supports this upgrade, it responds with a confirming response header and establishes the websocket connection.

  • Bidirectional communication:
    Once the websocket connection is established, both the client and the server can asynchronously send messages in both directions. This enables the server to send real-time updates or notifications to the client, and the client to send requests or information to the server without the need for a new HTTP request.

  • Low overhead:
    Websocket technology has lower communication overhead compared to the traditional HTTP protocol. This is because, once the websocket connection is established, there is no need to repeatedly send HTTP headers with each message, resulting in more efficient communication.

  • Support for different data types:
    Websocket supports the transmission of various data types, such as text and binary data. This allows for sending structured messages in JSON format, images, audio, video, and more.

  • Events and APIs:
    To handle websocket communication, both the client and the server implement APIs that provide events and methods for sending and receiving messages. In JavaScript, for example, in the browser, you can use the websocket API to establish and interact with a websocket connection.

Overall, websocket connections are widely used in real-time web applications such as real-time chats, push notifications, multiplayer games, and other scenarios where real-time communication is required. They offer an efficient and low-latency alternative to traditional HTTP-based communication.


websockets-communication

Overview

The purpose of the project was to demonstrate the operation of a simple websocket connection between a client and a web server. In this process, the client can connect to the web server and be in a position to receive messages (actions) from the server at any time, without the need for an external request (via the HTTP protocol).

In the proposed example, the web server exposes (in the websocket connection) an endpoint that triggers the sending of an action to the client; in this case, the action is to check the status of an account on the client's machine (demonstration example). Additionally, the server keeps a record of all executed actions for all connected clients, storing them in a list (array) called 'commandList', which could be easily replaced by a database table for data persistence. Overall, the handling of these actions, regardless of their nature, follows the same websocket connection approach.

Regarding the instances of socket connections for each client linked to the server, they are identified and stored within a variable named 'clients', of type map. Each connection created with the server is stored in the variable as a key-value pair, where the value represents the instance of the client's websocket associated with its client name upon connection. This allows for controlled message sending (actions) to each client or group of clients.

Installation (Step by Step)

Step 1 - Clone this github repository:

$ git clone https://github.com/0liveiraVictor/websockets-communication.git

Step 2 - Install client and server project dependencies

Server dependencies

  • In the root folder of the project access the server directory:
    $ cd server
    
  • Install all required dependencies for the server:
    $ npm install
    

Client dependencies

  • In the root folder of the project access the client directory:
    $ cd client
    
  • Install all required dependencies for the client:
    $ npm install
    

Environment Variables

The configuration of environment variables was defined in a .env file within each base application directory (server and client). Below are listed the parameters used as environment variables in each application:

Server params

Name Description
APP_PORT_SERVER Port on which the server application is running.
APP_HOST_SERVER Server application hostname.
SECRET_KEY_JWT_SERVER Secret key used in creating server JWT token.
SECRET_KEY_JWT_CLIENT Secret key used in decoding the client's JWT token.
EXPIRES_IN_JWT_SERVER Value for expiry of JWT token created on server.
OBJECT_COMMAND_NAME Name of run command.
OBJECT_COMMAND_SQL SQL execution command.
OBJECT_COMMAND_FIELD Attribute (field/column) of the command.
OBJECT_COMMAND_FILTER Comparison operator used to filter the command.
OBJECT_COMMAND_STATUS Command status [default = OPEN].

Client params

Name Description
URL_WS_CLIENT URL used by client to establish websocket connection; e.g. ws://[server_hostname]:[server_port]?name=[client_name].
SECRET_KEY_JWT_CLIENT Secret key used in creating client JWT token.
SECRET_KEY_JWT_SERVER Secret key used in decoding the server's JWT token.
EXPIRES_IN_JWT_CLIENT Value for expiry of JWT token created on client.
RECONNECTION_INTERVAL Time interval (ms) for client reconnection in case of server connection drop [default = 5000].

Running (Step by Step)

Step 1 - Run the server

  • In the root folder of the project access the server directory:
    $ cd server
    
  • Turn on the server by running the script:
    $ npm start
    

Step 2 - Run the client

For each client that wishes to connect to the websocket server, it will be necessary to run in separate terminals. To do this, open a new terminal (or multiple client) and follow the steps:

  • In the root folder of the project access the client directory:
    $ cd client
    
  • Connect to the websocket server:
    $ npm start
    

Technologies Used

Languages:

Web execution platform:

Package manager:

Dependency libraries:

Author

Made by Victor Oliveira.

GitHub LinkedIn

0liveiraVictor

License

ISC License

Copyright (c) 2023 VICTOR OLIVEIRA

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

About

Simple example of communication via websockets.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published