All Projects → batiwo → spring-websocket-angular6

batiwo / spring-websocket-angular6

Licence: other
Example for using Spring Websocket and Angular with Stomp Messaging

Programming Languages

typescript
32286 projects
java
68154 projects - #9 most used programming language
javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to spring-websocket-angular6

AuthGuard
Example repo for guarding routes post
Stars: ✭ 42 (+133.33%)
Mutual labels:  angular2, angular4, angular5, angular6
Ng Simple Slideshow
A simple, responsive slideshow for Angular 4+.
Stars: ✭ 119 (+561.11%)
Mutual labels:  angular2, angular4, angular5, angular6
Nebular
💥 Customizable Angular UI Library based on Eva Design System 🌚✨Dark Mode
Stars: ✭ 7,368 (+40833.33%)
Mutual labels:  angular2, angular4, angular5, angular6
Ngx Monaco Editor
Monaco Editor component for Angular 2 and Above
Stars: ✭ 347 (+1827.78%)
Mutual labels:  angular2, angular4, angular5, angular6
Aspnetcore Angular Universal
ASP.NET Core & Angular Universal advanced starter - PWA w/ server-side rendering for SEO, Bootstrap, i18n internationalization, TypeScript, unit testing, WebAPI REST setup, SignalR, Swagger docs, and more! By @TrilonIO
Stars: ✭ 1,455 (+7983.33%)
Mutual labels:  angular2, angular4, angular5, angular6
Angular Material App
基于最新Angular 9框架与Material 2技术的web中后台前端应用框架。
Stars: ✭ 509 (+2727.78%)
Mutual labels:  angular2, angular4, angular5, angular6
Ng2 Pdfjs Viewer
An angular 8 component for PDFJS and ViewerJS (Supports angular 2/4/5/6/7)
Stars: ✭ 150 (+733.33%)
Mutual labels:  angular2, angular4, angular5, angular6
angular-rollbar-source-maps
Angular 2+ implementation to upload sourcemaps to Rollbar
Stars: ✭ 17 (-5.56%)
Mutual labels:  angular2, angular4, angular5, angular6
Angular File Uploader
Angular file uploader is an Angular 2/4/5/6/7/8/9/10 + file uploader module with Real-Time Progress Bar, Responsive design, Angular Universal Compatibility, localization and multiple themes which includes Drag and Drop and much more.
Stars: ✭ 92 (+411.11%)
Mutual labels:  angular2, angular4, angular5, angular6
Ng2 Flatpickr
Angular 2+ wrapper for flatpickr (https://github.com/chmln/flatpickr)
Stars: ✭ 91 (+405.56%)
Mutual labels:  angular2, angular4, angular5, angular6
Ng Http Loader
🍡 Smart angular HTTP interceptor - Intercepts automagically HTTP requests and shows a spinkit spinner / loader / progress bar
Stars: ✭ 327 (+1716.67%)
Mutual labels:  angular2, angular4, angular5, angular6
Angular5 Seed
Angular5 Seed for Application
Stars: ✭ 222 (+1133.33%)
Mutual labels:  angular2, angular4, angular5, angular6
Ngx Smart Modal
Modal/Dialog component crafted for Angular
Stars: ✭ 256 (+1322.22%)
Mutual labels:  angular2, angular4, angular5, angular6
Angular Froala Wysiwyg
Angular 4, 5, 6, 7, 8 and 9 plugin for Froala WYSIWYG HTML Rich Text Editor.
Stars: ✭ 696 (+3766.67%)
Mutual labels:  angular2, angular4, angular5, angular6
angular-progress-bar
This component allow you to easy incorporate progress-bar to angular/ionic project, providing binding and color options
Stars: ✭ 26 (+44.44%)
Mutual labels:  angular2, angular4, angular5, angular6
Ngx Papaparse
Papa Parse wrapper for Angular
Stars: ✭ 83 (+361.11%)
Mutual labels:  angular2, angular4, angular5, angular6
ng2-STOMP-Over-WebSocket
STOMP Over WebSocket service for angular2
Stars: ✭ 35 (+94.44%)
Mutual labels:  angular2, websockets, sockjs, stomp
ng4-stompjs-demo
A sample using Angular4, Angular CLI and @stom/ng2-stompjs
Stars: ✭ 20 (+11.11%)
Mutual labels:  angular2, angular4, stomp, stompjs
Springbootangularhtml5
♨️ Spring Boot 2 + Angular 11 + HTML5 router mode + HTTP interceptor + Lazy loaded modules
Stars: ✭ 89 (+394.44%)
Mutual labels:  angular2, angular4, angular5, angular6
Ngx Daterangepicker Material
Pure Angular 2+ date range picker with material design theme, a demo here:
Stars: ✭ 169 (+838.89%)
Mutual labels:  angular2, angular4, angular5, angular6

Spring WebSocket Angular6

The purpose of this project is to present a full example of notification/messaging service between an angular6 client and a spring application. There are many examples of clients using SockJS but with javascript directly and not angular (Examples).

This project is based on ng2-stompjs (Please consult its great documentation). But it aim to provide a complete and minimalist project to serve as plug'n'play project for both frontend and backend.

Usage

  1. Clone the repository git clone https://github.com/batiwo/spring-websocket-angular6.git
  2. Build the angular-websocket project with npm install to install dependencies and then ng build -base-href=.
  3. Build the spring-websocket project with mvn install
  4. Run the produced JAR spring-websocket-1.0-SNAPSHOT.jar with java -jar spring-websocket-1.0-SNAPSHOT.jar
  5. Go to http://localhost:8080/index.html and enjoy.

Screenshot of running demo

How to

Spring Server

The spring server is a basic spring-boot api with the spring-websocket dependency.

A Simple Configuration

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

The WebSocketConfiguration is also minimalist. We just configure the MessageBroker with an Endpoint and setAllowedOrigins to anyone. The Endpoint "/socket" means that you will connect to the ws://server-url/socket with your clients.

@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/socket").setAllowedOrigins("*");
    }
}

Notice that we do not use withSockJS()

registry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS();

A Simple Controller

A method that is called when a message is sent from the client (angular) to the server (spring)

@MessageMapping(RECEIVING_URL)
public void onReceivedMessage(String message) {
    System.out.println("New message received : " + message);
}

A method called each 1000ms that send a string message

@Scheduled(fixedRate = 1000)
public void sendMessage() {
    template.convertAndSend(SENDING_URL, buildNextMessage());
}

An optional but interesting method that send a message just after the client subscribed (or re-subscribed)

@SubscribeMapping(SENDING_URL)
public String onSubscribe() {
    return "SUBSCRIBED : " + message;
}

Angular Client

Step to re-create the angular client :

Prerequisites
  1. Create a new angular application ng new angular-websocket
  2. Install required dependencies npm install @stomp/ng2-stompjs and npm install rxjs-compat
  3. Create a service ng g service services/messaging
  4. Then develop the application code.
Sample explanation

Here is some useful import

import { StompService, StompConfig, StompState } from "@stomp/ng2-stompjs";
import { Message } from "@stomp/stompjs";
import { Observable, BehaviorSubject } from "rxjs";

Create Stomp Configuration

let stompConfig: StompConfig = {
  url: socketUrl,
  headers: {
    login: "",
    passcode: ""
  },
  heartbeat_in: 0,
  heartbeat_out: 20000,
  reconnect_delay: 5000,
  debug: true
};

Create Stomp Service

this.stompService = new StompService(stompConfig);

Connect to a Stream

this.stompService.subscribe(streamUrl);

Send a message from client (angular) to the server (spring)

this.stompService.publish(url, JSON.stringify(message));

Monitor the state this.stompService.state

Contributor

Sébastien DRUJONT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].