Code Monkey home page Code Monkey logo

signalrchatapp's Introduction

SignalR Chat App

This is a backend of chat app inspired by Slack using .Net 8 ASP.NET MVC and SignalR to provide RESTful API and websocket real-time communication.

Author

Features

  • User authentication
  • Real-time chat
  • Create, join, leave, delete channels
  • Send messages in channels
  • Upload files

Technologies

  • .Net 8
  • ASP.NET MVC
  • SignalR
  • MS SQL Server
  • Entity Framework Core
  • Identity Framework
  • JWT

Authorization

  • This app uses JWT for authorization. To access RestfulAPI, you need to pass Bearer token to the Authorization header of request and access_token=[token] to the parameter of request to access SignalR hub.
  • To get the token, you need to login to the app and get the token from the response. To more detail about login, check below.

API Reference

  • Root Rest API URL: 'host/api'

Account

  • Login:
    • Get Bearer Token from username and password
    • Method: POST
    POST /account/login
    Body:
      {
          "UserName": "string",
          "Password": "string"
      }
    • Response:
    {
       "username": "maxrave",
       "token": "abcxyz",
       "expiresIn": "2022-12-31T23:59:59.999Z"
    }
    • If error:
    {
       "message": "Invalid credentials",
       "errors": true
    }
  • Register:
    • Register new user
    • Method: POST
    POST /account/register
    Body:
       {
        "FullName": string
        "UserName": string
        "Email": string
        "Password": string
       }
    • Response:
    {
       "message": "User created successfully!",
       "errors": false
    }
  • Logout:
    • Log out and remove token from backend
    • Method: POST
    POST /account/logout
    • Response:
     STATUS 200 OK
    
  • User:
    • Get user info
    • Method: GET
    GET /account/user
    • Response:
    {
      "fullName": "string",
      "userName": "string",
      "avatar": "string",
      "rooms": [],
      "messages": [] 
    }

Rooms

  • Get Rooms:

    • Get all rooms
    • Method: GET
    GET /rooms
    • Response:
    [
      {"id":1,"name":"DEPLOYED ROOM","admin":"maxrave"},
      {"id":2,"name":"ROOM 1","admin":"maxrave"},
      {"id":3,"name":"ROOM 2","admin":"maxrave"}
    ]
  • Create Room:

    • Create new room
    • Method: POST
    POST /rooms
    Body:
    {
      "name": "string"
    }
    • Response:
    {
      "id": 1,
      "name": "string",
      "admin": "string"
    }
  • Remove Room:

    • Remove room (only admin of this room can remove)
    • Method: DELETE
    DELETE /rooms/{roomId}
    • Response:
    STATUS 200 OK
    

Messages

  • Get Messages:

    • Get all messages in room
    • Method: GET
    GET /Messages/Room/${roomName}
    • Response:
    [
        {
            "id": 1,
            "content": "xin zu m oi",
            "timestamp": "2024-04-30T16:07:48.8828629",
            "fromUserName": "maxrave",
            "fromFullName": "Nguyen Duc Tuan Minh",
            "room": "DEPLOYED ROOM",
            "avatar": null
        },
        {
            "id": 2,
            "content": "dung dc on dung ko",
            "timestamp": "2024-04-30T22:22:16.0819198",
            "fromUserName": "maxrave",
            "fromFullName": "Nguyen Duc Tuan Minh",
            "room": "DEPLOYED ROOM",
            "avatar": null
        },
        {
            "id": 3,
            "content": "hi",
            "timestamp": "2024-05-01T12:54:38.4343108",
            "fromUserName": "maxrave",
            "fromFullName": "Nguyen Duc Tuan Minh",
            "room": "DEPLOYED ROOM",
            "avatar": null
        }
    ]
  • Send Message:

    • Send message to room
    • Method: POST
    POST /Messages/
    Body:
    {
      "RoomName": string;
      "Content": string;
    }
    • Response:
    {
      "id": 4,
      "content": "alo",
      "timestamp": "2024-05-01T12:57:09.7532232+07:00",
      "fromUserName": "maxrave",
      "fromFullName": "Nguyen Duc Tuan Minh",
      "room": "DEPLOYED ROOM",
      "avatar": null
    }
  • Upload File:

    • Upload file to room
    • Must add "Content-Type" : "multipart/form-data" to header
    • Type of file: .jpg,.jpeg,.png, size: <= 1048576 bytes
    • Method: POST
    POST /upload
    Body:
    {
      RoomId: number;
      BackendHost: string; //This host will be used to get file content
      File: File;
    }
    • Response:
    STATUS 200 OK
    
  • Delete Message:

    • Delete message (only sender of this message can delete)
    • Method: DELETE
    DELETE /Messages/{messageId}
    • Response:
    STATUS 200 OK
    
  • Websocket:

    • Connect to SignalR hub using access_token=[token] as query parameter
    • Recommend using SignalR client to connect to hub.
    • Hub URL: host/chatHub
    • Methods:
      • Join', roomName: string - Join room
      • onRoomDeleted - Receive message when room is deleted
      • newMessage, return message: Message - Receive new message
      • removeChatMessage, return id: number - Receive the id of deleted message

Installation

  • Clone this repository
  • Update appsettings.json with your connection string
  • Run dotnet ef database update to create database
  • Run dotnet run to start the app

signalrchatapp's People

Contributors

maxrave-dev 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.