Giter Site home page Giter Site logo

ng2-websockets's Introduction

ng2-websockets

Example project of using WebSockets in an Angular 2 application taken from https://github.com/PeterKassenaar/ng2-websockets

It has just been edited to add the close of the WS after leaving the view.

Original description: This project consists of two parts, a client (angular 2.4, as of Feb.02.2017) and server part (Nodejs 4.x+).

Server(s)

  • Download or clone the repo and run npm install in the server directory to install server dependencies.
  • Currently the server only has a dependency on js-websockets
  • Run node server.js or nodemon server.js to start the server.
  • A chat server is started on localhost:3005.
  • A random data emitter is started on localhost:3006 (community: can this be done simpler??)

The only purpose of this simple server is to start a websocket server and accept incoming connections. Once a message is posted to the server, it is transformed by adding the local time to the incoming object and then echoed back to the client.

The other server emits a random number, every 1 second. It is displayed in the user interface. Think of this as an example for a "stock ticker", or the like.

Client

  • Download or clone the repo and run npm install in the client directory to install dependencies.
  • Make sure the server is already started before you try to start the client (as it is trying to connect to the ws-server on startup).
  • run npm start to start the client.
  • Typ a message in the messagebox. It should reappear in the list of messages.

Messages are always in the format of a Message-object in the following format:

interface Message {
	author: string,
	message: string,
	newDate?: string
}

the field newDate is added by the server.

The number server emits just a simple random number 0 - 10,000.

Credits

This project is based on previous work by Łukasz Wojciechowski (https://github.com/lwojciechowski/mildchat-client), who also wrote [this great article] (https://medium.com/@lwojciechowski/websockets-with-angular2-and-rxjs-8b6c5be02fac#.v1d65auh8) on Angular 2 and websockets.

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.