Giter VIP home page Giter VIP logo

websocket-chat-server's Introduction

WebSocket server for chat application

This is the beginning of my websocket server with socket.io, express and typescript

Websocket client at: chat client

Docs (sort of...)

The whole logic of the project lives on the index.ts file, here we start an Express server and a Socket.io server instance.

The Socket.io server instance, has available a CORS endpoint, which is set to my client deployment when on production, or localhost:3000 at development.

Upon connection io.on("connecion", () => {}), we instantiate all the event listeners for the client to call later on.

User store

This class is basically our in memory database, which will store the users, as long as they are using the chat.

The UserStore class, lives in the userStore.ts file. It contains all the active users in an map, and has different methods to interact with them.

  • findUser, which takes the userID and returns the user if exists
  • saveUser, which takes the user and saves it if it did not exist before.
  • deleteUser, which takes the userID and deletes the user if existent
  • getRoomUsers, which takes the room and returns an array of users in that room

Events

Login

When a login event is triggered, the server receives the selected name and room of the user (both strings) and a callback function to let the client know if there were any errors at login, as parameters.

All parameters are passed through the corresponding joi schema validation to ensure that the values are the expected. Should an error occur in the validation process, the callback function would be sent to the client with said error return callback({ error: "__error to return here__", user: undefined });

After validation, the server stores the socket.id along with the name and room to create a user object in the UserStore class.

Finally, we join the user/socket to the requested room, notify all the users in the room, besides the user/socket, that a new user is joining, with the notification event. We send to all the room users, the updated array of room users, with the usersevent, and we return the callback function with no errors to indicate the client that the have successfully logged in.

Logout

When a logout event is triggered, the server receives a callback as a parameter to make the client know the have logged out successfully. First we remove the user from the UserStore, we remove the user/socket from the room, and we notify the remaining room users, that this user is gone, with the notification event. Finally we send the updated user list with the users event, and call the callback function to let the client know that all went good.

Room message

When the roomMessage event is triggered, the server receives the encrypted message from the client, gets the user data from the UserStore.findUser(userid) call, and sends the message to the user's room, with the roomMessage event.

Ping

Mostly for debugging purposes, but it returns the socket's id and runs the callback function, which in the client, it will tell the amount of ping between the client and the server.

websocket-chat-server's People

Contributors

juancortelezzi avatar

Watchers

James Cloos avatar  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.