Git Product home page Git Product logo

hacky-simple-chat-server's Introduction

Dependencies:

  • node 16
  • yarn

It installs own version of TypeScript

Install using yarn, then run yarn start to start.

There is a hacky client in testclient, just open client.html in browser (after yarn start).

Brief summary of happy path

Connecting with WebSocket

Default listening port is 25123, you can use

  const ws = new WebSocket("ws://localhost:25123");

to attempt connection on localhost. Keep in mind that the server doesn't support secure websockets (wss) protocol (you can wrap it with nginx upgrade passthrough, but it exceeds scope of this repo).

Next step is to wait for "open" on connection (it is a good idea to bind "close" and "error" events to a function that will attempt reconnection)

  ws.addEventListener("open", () => {
    // you can continue your logic here
  }, { once: true })

Yay, you are set now.

WebSocket communication

Server reacts only to stringified messages (JSON.stringify(eventData))

Hello event

  • Kind hello
  • Data
    • name
      • type: string
      • description: The name that user will be introduced to the server (sets name serverside)
  • Example
{
  kind: "hello",
  data: {
    name: "test user"
  }
}
  • Effects
    • Server will echo
    • Example
{
  kind: "hello",
  data: {
    name: "test user"
  }
}

User type

  • Note: Nothing is validated
  • Fields
    • name
      • type: string
      • description: name of the user (can be an empty string)

GetUsers event

  • Kind getUsers
  • Data Data is null for this event, server will ignore data if sent
  • Example
{
  kind: "getUsers",
  data: null
}
  • Effects
    • Server will reply with event
      • Kind getUsers
      • Data
        • users
          • type: array of User
          • description: all users that are stored serverside
      • Example
{
  kind: "getUsers",
  data: {
    users: [
      {
        name: "Daniel"
      },
      {
        name: "Maciej"
      }
    ]
  }
}

RawMessage type

  • Note: Nothing is validated, author and text are never checked if they are actual authors
  • Fields
    • author
      • type: string
      • description: name of user who sent the message
    • text
      • type: string
      • description: body of the message

GetMessages event

  • Kind getMessages
  • Data Data is null for this event, server will ignore data if sent
  • Example
{
  kind: "getMessages",
  data: null
}
  • Effects
    • Server will reply with event
      • Kind getMessages
      • Data
        • messages
          • type: array of RawMessage
          • description: all messages that are stored serverside
      • Example
{
  kind: "getMessages",
  data: {
    messages: [
      {
        author: "Daniel",
        text: "Hi, how is the course going?"
      },
      {
        author: "Maciej",
        text: "Great!"
      }
    ]
  }
}

NewMessage event

  • Kind newMessage
  • Data
    • type: RawMessage
  • Example
{
  kind: "newMessage",
  data: {
    author: "Daniel",
    text: "I'm happy to hear that"
  }
}
  • Effects
    • Server will broadcast echo of this event (including the sender)
    • Example
{
  kind: "newMessage",
  data: {
    author: "Daniel",
    text: "I'm happy to hear that"
  }
}

You can check /src/Client/ClientEvent.ts in order to see available events. You can copy-paste this file to your project if you want to get headstart with typings. If you will want that - ClientSide are events that you will send, ServerSide are events that you will receive.

hacky-simple-chat-server's People

Contributors

danielduel avatar

Stargazers

 avatar

Watchers

 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.