Giter Club home page Giter Club logo

luixaviles / socket-io-typescript-chat Goto Github PK

View Code? Open in Web Editor NEW
822.0 822.0 277.0 19.21 MB

A chat application implemented using Socket.io, TypeScript, Angular and Angular Material components

Home Page: https://medium.com/dailyjs/real-time-apps-with-typescript-integrating-web-sockets-node-angular-e2b57cbd1ec1

License: MIT License

JavaScript 5.68% TypeScript 74.86% CSS 3.33% HTML 16.13%
angular angular-material2 chat nodejs socket typescript

socket-io-typescript-chat's Introduction

GitHub stars Tweet

A Socket.io Chat Example Using TypeScript

This repository contains server & client side code using TypeScript language

Blog Post

Read the blog post with details about this project: Real Time Apps with TypeScript: Integrating Web Sockets, Node & Angular

Live Demo

Try live demo: https://typescript-chat.firebaseapp.com

Support this project

  • Star GitHub repository โญ
  • Create pull requests, submit bugs or suggest new features
  • Follow updates on Twitter or Github

Running Server and Client locally

Prerequisites

First, ensure you have the following installed:

  1. NodeJS - Download and Install latest version of Node: NodeJS
  2. Git - Download and Install Git
  3. Angular CLI - Install Command Line Interface for Angular https://cli.angular.io/

After that, use Git bash to run all commands if you are on Windows platform.

Clone repository

In order to start the project use:

$ git clone https://github.com/luixaviles/socket-io-typescript-chat.git
$ cd socket-io-typescript-chat

Run Server

To run server locally, just install dependencies and run gulp task to create a build:

$ cd server
$ npm install -g gulp-cli
$ npm install
$ gulp build
$ npm start

The socket.io server will be running on port 8080

When you run npm start, this folder leverages nodemon which will automatically reload the server after you make a change and save your Typescript file. Along with nodemon, there is also a gulp watch task that you can run to reload the files but it's not necessary and is provided merely as a teaching alternative.

Run Angular Client

Open other command line window and run following commands:

$ cd client
$ npm install
$ ng serve

Now open your browser in following URL: http://localhost:4200

Server Deployment

Take a look the Wiki Page for more details about deploying on Heroku and Zeit.co.

Feel free to update that page and Readme if you add any other platform for deployment!

Forks

The Open Source community is awesome! If you're working in a fork with other tech stack, please add the reference of your project here:

Features Author Status
React + TypeScript + Material-UI client nilshartmann In Progress

Contribution

Contributions are greatly appreciated. You can contribute by adding i18n support with your language, the testing section or any other feature.

Contributors

luixaviles hughanderson4 ultrarunner theIDinside carmius Hellmy
luixaviles hughanderson4 ultrarunner theIDinside carmius Hellmy
you
You

License

MIT

socket-io-typescript-chat's People

Contributors

carmius avatar dependabot[bot] avatar dianjvm avatar firstvertex avatar harshilparmar avatar kenionatan avatar luixaviles avatar oscarscholten avatar suhailvs avatar theidinside avatar ultrarunner avatar velaia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

socket-io-typescript-chat's Issues

Node v14.2.0 Errors in server

Hello. I have created an application from your example and the express server keeps crashing and restarting when a socket.on event happens. This may be a false alarm and a bug in my application, but if anyone could help me decipher the error message, that would be great.
~/.pm2/pm2.log

2020-05-12T14:58:22: PM2 error: TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (7679)
    at Object.writeFileSync (fs.js:1380:5)
    at /usr/lib/node_modules/pm2/lib/God/ForkMode.js:264:12
    at wrapper (/usr/lib/node_modules/pm2/node_modules/async/internal/once.js:12:16)
    at next (/usr/lib/node_modules/pm2/node_modules/async/waterfall.js:96:20)
    at /usr/lib/node_modules/pm2/node_modules/async/internal/onlyOnce.js:12:16
    at WriteStream.<anonymous> (/usr/lib/node_modules/pm2/lib/Utility.js:186:13)
    at WriteStream.emit (events.js:315:20)
    at WriteStream.EventEmitter.emit (domain.js:547:15)
    at internal/fs/streams.js:395:12
    at FSReqCallback.oncomplete (fs.js:163:23)

My "chat-server.ts" : https://github.com/xinthose/angular_cpp_websocket/blob/master/server/src/portalogic-server.ts
Target: es2019
OS: Ubuntu 18.04 LTS

This may be a bug with express or socket-io with node v14? I do not have problems with node v13.

socket.io-client dependency version mismatch

It seems socket.io-client is only available in the client project because karma defines it as a dependency. This will not allow to explicit define the socket.io-client version to be used.
Additionally, the server defines the socket.io dependency as socket.io": "~2.0.3. [email protected] is using [email protected], which already is a major version mismatch.

Maybe the client should use the socket.io-client the server is serving, or at least use socket.io as a defined dependency in the clients package.json

what the code inside index.js?

Hi sir, i want to trying make an chatting application inside on my existing application.
i just want to ask to you what the code inside index.js ?? from your Procfile

web: node server/dist/index.js

thank you.

Cannot find name 'Symbol'

C:\node_apps\socket-io-typescript-chat\server>gulp build
[11:31:37] Using gulpfile C:\node_apps\socket-io-typescript-chat\server\gulpfile
.js
[11:31:37] Starting 'build'...
Compiler option 'compileOnSave' requires a value of type boolean.
C:/node_apps/socket-io-typescript-chat/server/node_modules/@types/node/index.d.t
s(6903,88): error TS2304: Cannot find name 'Symbol'.
[11:31:39] TypeScript: 1 semantic error
[11:31:39] TypeScript: emit succeeded (with errors)
[11:31:39] Finished 'build' after 1.89 s

Error importing material modules

When trying to build the client side, one gets a lot of errors, regarding the importing of material modules.
Currently, they're for example referenced by

import {MdButtonModule} from '@angular/material'

When following the build instructions, one gets errors like this, when trying to 'ng serve':

ERROR in Error: MdButtonModule is not an NgModule
    at _getNgModuleMetadata (/home/cx/workspace/es/socket-io-typescript-chat/client/node_modules/@angular/compiler-cli/src/ngtools_impl.js:140:15)
    at _extractLazyRoutesFromStaticModule (/home/cx/workspace/es/socket-io-typescript-chat/client/node_modules/@angular/compiler-cli/src/ngtools_impl.js:109:26)
    at /home/cx/workspace/es/socket-io-typescript-chat/client/node_modules/@angular/compiler-cli/src/ngtools_impl.js:129:27
    at Array.reduce (<anonymous>)
    at _extractLazyRoutesFromStaticModule (/home/cx/workspace/es/socket-io-typescript-chat/client/node_modules/@angular/compiler-cli/src/ngtools_impl.js:128:10)
    at Object.listLazyRoutesOfModule (/home/cx/workspace/es/socket-io-typescript-chat/client/node_modules/@angular/compiler-cli/src/ngtools_impl.js:53:22)
    at Function.NgTools_InternalApi_NG_2.listLazyRoutes (/home/cx/workspace/es/socket-io-typescript-chat/client/node_modules/@angular/compiler-cli/src/ngtools_api.js:91:39)
    at AotPlugin._getLazyRoutesFromNgtools (/home/cx/workspace/es/socket-io-typescript-chat/client/node_modules/@ngtools/webpack/src/plugin.js:241:66)
    at _donePromise.Promise.resolve.then.then.then.then.then (/home/cx/workspace/es/socket-io-typescript-chat/client/node_modules/@ngtools/webpack/src/plugin.js:495:24)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
ERROR in /home/cx/workspace/es/socket-io-typescript-chat/client/src/app/dialog-user/dialog-user.component.ts (2,23): Module '"/home/cx/workspace/es/socket-io-typescript-chat/client/node_modules/@angular/material/material"' has no exported member 'MD_DIALOG_DATA'.

Socket connection issue after heroku deployment

When I deploy my website to heroku app everytime it is connecting to new port

In the client code, we are adding static port
const SERVER_URL = 'http://localhost:8080';

public initSocket(): void {
this.socket = socketIo(SERVER_URL);
}
Therefore, after deploying the website on heroku, socket is not working.

Could you please tell me how you made it possible on heroku deployment.
Is it due to I am using free service of heroku (may be). I dont know the reason.

Thanking you in advance.

Browser screenshots.

1st one is Firefox 55.0.3 (64-bit) Win 10 Pro 15063.540

fireshot capture 2 - socket io typescript chat example - https___typescript-chat firebaseapp com_

2nd one is Vivaldi 1.11.917.43 (32-bit) same Windows system

vb-fireshot pro screen capture 321 - socket_io typescript chat example

No current clean method of getting the embedded conversation in a screenshot. HTH.

working on the server

@luixaviles when you're working on the server, have you setup a watch script?

I went through the gulpfile and I don't see the ability to make changes, save, and have the server update.

I did some digging and found this stackoverflow thread - as the author describes, it's not perfect but it works.

So I've installed 'ts-node' and updated your gulpfile to work with this project - and it works which is most of the battle - just want to get your feedback if you have a less hacky solution?

var gulp = require('gulp'),
  nodemon = require('gulp-nodemon'),
  livereload = require('gulp-livereload'),
  ts = require('gulp-typescript');

var tsProject = ts.createProject('tsconfig.json');

gulp.task('typescript', function() {
  console.log('Compiling TypeScript');

  return tsProject
    .src()
    .pipe(tsProject())
    .js.pipe(gulp.dest('./dist'));
});

gulp.task('serve', ['typescript'], function() {
  gulp.watch('./**/*.ts', ['typescript']);

  livereload.listen();
  // make sure you have installed ts-node via npm i ts-node
  nodemon({
    exec: 'ts-node ./src/index.ts',
    ext: 'ts'
  }).on('restart', function() {
    setTimeout(function() {
      console.log('reload!');
      livereload.reload();
    }, 500);
  });
});

Production

Is the application is production ready for android ? please guide ? can we integrate the firebase ? how ? how to send the queries to Linux server ?

App Not Loading on Safari and ios mobile browsers

I know this issue might not be served well here but I know this app was deployed successfully on Heroku by the creator so hoping to get some insight.

I build and deployed the app to Heroku, I am just using the free dyno and the site works well on mhy windows machines and on my mac (chrome and firefox). The app however wont load on Safari or any of my iPhone mobile browsers. I have tried to do some research and I am getting a whole lot of nothing. The most I can come up with is iOS doesnt like the certificate heroku includes in their free dyno.

here is a link to my stackoverflow question which included a photo of the dev console from safari

https://stackoverflow.com/questions/52820905/angular-6-app-wont-load-on-mobile-browser-iphone-or-safari

Does this sound right?

Sorry again for posting this here as this is a production issue and not an issue with the repo itself. Just been dealing with this for a few days and its starting to drive me crazy.

THANKS for any help!

How to test the server

Hi,
I am trying to figure out how to test the server side. I am using mocha, chai and sinon but really don't know how to emulate a websocket client on the server side so that I can test its flow. Could you help or suggest what client to use to initiate connect from the test?

Thank you.
Kind regards,
Jarek

What is the room name people are joining the chat, how to change it in socket io and read roomName from url?

Great work and very nice tutorial.

  1. I am trying to experiment this by having 2 users connected 1-1 instead of everyone in the same common room. How can i add 2 users in 1 room once both of them are on the page, if 3rd user comes, we put them on waiting and when 4th comes, 3rd and 4th are added to that room to have private chat? Kind of random 1-1 chat with 2 people at 1 time. i have checked your tutorial but couldn't find the room name where you are emitting message and join events, as per socket io, events are emitted in rooms, https://socket.io/docs/emit-cheatsheet/ how is your approach different here?

  2. Alternatively to be simple, how can i put let's say room name in the url, so that it loads that specific room, for e.g if i put http://localhost:4200?room=typescript-chat-room this should load a room named, typescript-chat-room and if i do http://localhost:4200?room=angular-discussion then that should connect to the room named, 'angular discussion' and so on. Please help me point in the code where i can change the room in server and how can i pass it as a query param from frontend client? i'd like to contribute once i understand the basics via code for rooms logic.

  3. Also, is there reconnection implemented if socket io looses connection , does this auto reconnect the users? As i see socket io looses lots of connectivity due to ping timeouts, transport errors etc, is there any way to reconnect the chat automatically due to any network failure without page refresh?

  4. what steps would be needed to allow users to upload and share videos/images/audio msgs (files) in the chat? can u share with me some libraries or examples to do this? i m interested in doing this.

need chat in https

this chat server does not work of https .can you share the code for the same

Question... CORS issue

Hello, I'm building a clone of your app, following along with the blog, and i'm getting this error in my console:

Failed to load http://http/socket.io/?EIO=3&transport=polling&t=M82Q3LZ: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5050' is therefore not allowed access.

But in my network tab, everything is coming through as OK 200...

Any ideas why this is happening? I git cloned this repo, and no errors come up when I run it, and my code looks exactly like yours minus the material style, and I don't have any of the new stuff you added to the project, just what I got from following along with the blog post

Cannot find namespace 'server'

The following error is thrown when running in a fresh setup:

src/server.ts(65,10): error TS2503: Cannot find namespace 'server'.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] build-ts: `tsc`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] build-ts script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

tsc --version
Version 2.4.2

node --version
v8.3.0

I think I might misunderstand something about TypeScript's namespaces. Would you be able to help?

On the Server side - Cannot GET /

First of all, it is a great application! Thank you.

On the client side, everything works fine. I can start the server on localhost:8080 and I see all the conversation of the diverse localhost:4200s in the server terminal. But in the browser localhost:8080 (server) it is written Error: "Cannot GET /". What could be the issue? How can I fix this error?
I have downloaded your files from GitHub and the terminal (server) looks fine (see screenshot).

Many thanks for your help!
Karin
screen shot 2018-08-28 at 22 56 00

Stupid question

What happens if "hacker" client sends a message which does not match message.model ?

Suggestion: auto scroll down message list

When a new message is received, the message list doesn't autoscroll to the bottom of the list, making the new message invisible if there is a scrollbar in <mat-list/>.

Suggestion: auto-scroll the message list to the bottom when a new message is received.

I had a quick look at the source code but am not sure how to do that in a clean angular-2 way.

what is the type of socket

I spy an unnecessary use of any

           this.io.on('connect', (socket: any) => {
            console.log('Connected client on port %s.', this.port);
            socket.on('message', (m: Message) => {
                console.log('[server](message): %s', JSON.stringify(m));
                this.io.emit('message', m);
            });

            socket.on('disconnect', () => {
                console.log('Client disconnected');
            });

what is socket?? what is the type? I can't find that info.. if I find out, I would like submit a PR for this lib if that's ok

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.