Last active
April 12, 2022 19:08
-
-
Save marweck/37903809a4345330bad29fcc0d277d5e to your computer and use it in GitHub Desktop.
SockJS service example for Angular 6+ applications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //... | |
| import { StompRService } from '@stomp/ng2-stompjs'; | |
| //... | |
| @NgModule({ | |
| //... | |
| providers: [ | |
| //... | |
| StompRService, | |
| //... | |
| }) | |
| export class AppModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| import { StompRService, StompConfig } from '@stomp/ng2-stompjs'; | |
| import { Message } from '@stomp/stompjs'; | |
| import * as SockJS from 'sockjs-client'; | |
| import { Observable } from 'rxjs'; | |
| /** | |
| * Must add dependency "@stomp/ng2-stompjs": "^6.0.1" | |
| */ | |
| @Injectable({ providedIn: 'root' }) | |
| export class SocketService { | |
| constructor(private stompService: StompRService) {} | |
| private init(): void { | |
| if (!this.stompService.connected()) { | |
| this.stompService.config = this.stompConfig(); | |
| this.stompService.initAndConnect(); | |
| } | |
| } | |
| onEvent(): Observable<Message> { | |
| this.init(); | |
| return this.stompService.subscribe('/event'); | |
| } | |
| private stompConfig(): StompConfig { | |
| const provider = function() { | |
| return new SockJS('/api/socket'); | |
| }; | |
| const config = new StompConfig(); | |
| config.url = provider; | |
| config.heartbeat_in = 0; | |
| config.heartbeat_out = 0; | |
| config.reconnect_delay = 10000; | |
| return config; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package infrastructure; | |
| import org.springframework.boot.SpringBootConfiguration; | |
| import org.springframework.messaging.simp.config.MessageBrokerRegistry; | |
| import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; | |
| import org.springframework.web.socket.config.annotation.StompEndpointRegistry; | |
| import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | |
| /** | |
| * Websocket configuration | |
| * <p> | |
| * Based on example https://medium.com/oril/spring-boot-websockets-angular-5-f2f4b1c14cee | |
| * It uses maven dependency <artifactId>spring-boot-starter-websocket</artifactId> | |
| */ | |
| @SpringBootConfiguration | |
| @EnableWebSocketMessageBroker | |
| class WebsocketConfiguration implements WebSocketMessageBrokerConfigurer { | |
| /** | |
| * Connection url exposed to clients | |
| */ | |
| @Override | |
| public void registerStompEndpoints(StompEndpointRegistry registry) { | |
| registry.addEndpoint("/api/socket").setAllowedOrigins("*").withSockJS(); | |
| } | |
| /** | |
| * Used by clients to send messages to server: /api/msg/* | |
| * <p> | |
| * Used by clients to listen to messages from server: /event | |
| */ | |
| @Override | |
| public void configureMessageBroker(MessageBrokerRegistry config) { | |
| config.setApplicationDestinationPrefixes("/api/msg").enableSimpleBroker("/event"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment