Giter VIP home page Giter VIP logo

socket-pong's Introduction

socket-pong

This contains a sample project consisting of a Node.js server and a client, which connect via websockets. The server sends "PING" messages to the client over the socket and the client is expected to respond with "PONG" messages to the server to keep the connection alive.

Requirements

Directory structure

Server

This directory contains code for the server. It consists of two main files, app.js and src/WebSocketServer.js

  1. app.js is the entry point to the server which consists of an HTTPServer and also initiates a websocket server
  2. src/WebSocketServer is the class that instantiates the websocket server object. It uses the ws package to implement the server. This class basically acts as a wrapper around the ws library, so that the library can be easily changed in the future without affecting the rest of the code

Client

This directory contains code for the client. It consists of two main files app.js and index.html

  1. app.js is the entry point to the client server which is used to render the main HTML page
  2. index.html is a file, which displays pings being received from the server, via websockets. It also contains a "Stop ponging" button to allow the client to stop ponging the server, which would ultimately result in the client's socket connection getting terminated in 5 seconds.

NOTE: The client can be run by directly opening the index.html file in the browser. A server was created for the same just for demonstration purposes, to show a more scalable scenario where a full fledged app is used to create such socket connections

How to run

We will require two terminal windows to run this. Please follow the following steps from the root folder of the repository:

Terminal 1

cd server
npm install
node ./app.js

Terminal 2

cd client
node ./app.js

There are logs generated on the server terminal window which help with seeing the status of various connections

API to see list of clients

Once the server has started, a list of the connected clients can be retrieved by calling http://localhost:8000/ with a GET request. The format of the response is as follows:

URL: http://localhost:8000/
METHOD: GET
RESPONSE:
{ "ids" : [ id1<String>, id2<String>, ... ]"

socket-pong's People

Contributors

anujk14 avatar

Stargazers

 avatar

Forkers

viniciusvinna

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.