{"id":20527,"date":"2021-01-14T13:23:31","date_gmt":"2021-01-14T12:23:31","guid":{"rendered":"http:\/\/www.highcharts.com\/blog\/?p=20527"},"modified":"2026-01-13T11:17:32","modified_gmt":"2026-01-13T11:17:32","slug":"live-data-with-rxjs-websocket-highcharts-and-angular","status":"publish","type":"post","link":"https:\/\/www.highcharts.com\/blog\/integration\/live-data-with-rxjs-websocket-highcharts-and-angular\/","title":{"rendered":"Live data with RxJs WebSocket, HighCharts, and Angular"},"content":{"rendered":"<p>&nbsp;<br \/>\nIn this tutorial, we will show you how to visualize real-time updates using <a href=\"https:\/\/rxjs-dev.firebaseapp.com\/api\/webSocket\/webSocket\" target=\"_blank\" rel=\"noopener\">RxJs Websocket<\/a> and the official <a href=\"https:\/\/github.com\/highcharts\/highcharts-angular\" target=\"_blank\" rel=\"noopener\">Highcharts Angular wrapper<\/a>. You can access the entire project by clicking on this <a href=\"https:\/\/github.com\/haroon786\/RxJs-WebSocket-Angular-HighCharts\" target=\"_blank\" rel=\"noopener\">GitHub link<\/a>.<\/p>\n<p><i><b>Remark<\/b><br \/>\nIn this article, random data are used to update the chart. There is no implementation of server-side code.<\/i><\/p>\n<p>There are two main parts to create this project:<br \/>\nThe first part is to set up an Angular project. For that, follow the standard instructions in this Angular guideline: <a href=\"https:\/\/angular.io\/guide\/setup-local\" target=\"_blank\" rel=\"noopener\">setting up the local environment and workspace<\/a>.<\/p>\n<p>The second part is to set up an Angular project with RxJsWebSocket and Highcharts by going through these three simple steps:<\/p>\n<p><b>Step 1<\/b><br \/>\nAs we will use the default protocol WSS(<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/WebSocket\" target=\"_blank\" rel=\"noopener\">WebSocket protocol<\/a>), the first step in the configuration project is to import the <code>WebSocket  import {webSocket}<\/code> from the <code>rxjs\/webSocket<\/code> package.<br \/>\nBy the way, if you are not familiar with WSS, here is a short explanation of what happens when there is a subscription to a socket:<br \/>\nThe <b>rxjs pipe<\/b> is a method that is used for composing operators. The Emitted values, that are received on subscription, are simply pushed in an array and assigned to data property, which is part of <code>chartoption<\/code>, in <code>app.component.ts<\/code>.<\/p>\n<pre>import {\r\n  Component,\r\n  OnInit\r\n} from '@angular\/core';\r\nimport * as Highcharts from 'highcharts';\r\nimport {\r\n  webSocket\r\n} from 'rxjs\/webSocket';\r\nimport {\r\n  of ,\r\n  Subscription\r\n} from 'rxjs';\r\nimport {\r\n  concatMap,\r\n  delay\r\n} from 'rxjs\/operators';\r\n\r\n@Component({\r\n  selector: 'app-root',\r\n  templateUrl: '.\/app.component.html',\r\n  styleUrls: ['.\/app.component.css']\r\n})\r\nexport class AppComponent implements OnInit {\r\n  title = 'Angular-RxJsWebSocket-HighCharts';\r\n  rate: any;\r\n  rate$: Subscription;\r\n  Highcharts: typeof Highcharts = Highcharts;\r\n  chardata: any[] = [];\r\n  chartOptions: any;\r\n  subject = webSocket('wss:\/\/ws.coincap.io\/prices?assets=bitcoin')\r\n  ngOnInit() {\r\n    this.rate = this.subject.pipe(\r\n      concatMap(item =&gt; of (item).pipe(delay(1000)))\r\n    ).subscribe(data =&gt; {\r\n      this.rate = data;\r\n      this.chardata.push(Number(this.rate.bitcoin))\r\n      this.chartOptions = {\r\n        series: [{\r\n          data: this.chardata,\r\n        }, ],\r\n        chart: {\r\n          type: \"line\",\r\n          zoomType: 'x'\r\n        },\r\n        title: {\r\n          text: \"linechart\",\r\n        },\r\n      };\r\n    })\r\n  }\r\n}<\/pre>\n<p><b>Step 2<\/b><br \/>\nOnce the project configuration is done; we have to access the Highcharts library to visualize the data update. To achieve that, we have to include the official Highcharts Angular wrapper (<code>highcharts-angular<\/code>), then import the Highcarts module:<\/p>\n<ul>\n<li>To install <code>highcharts-angular<\/code> and the Highcharts library, just run the following instruction: <code>npm install highcharts-angular highcharts<\/code>.<\/li>\n<li>To import the package go to <code>app.module.ts<\/code> file, and import the module <code>HighchartsChartModule<\/code> from the <code>highcharts-angular<\/code> package.<\/li>\n<\/ul>\n<pre>import {\r\n  BrowserModule\r\n} from '@angular\/platform-browser';\r\nimport {\r\n  NgModule\r\n} from '@angular\/core';\r\n\r\nimport {\r\n  AppRoutingModule\r\n} from '.\/app-routing.module';\r\nimport {\r\n  AppComponent\r\n} from '.\/app.component';\r\nimport {\r\n  HighchartsChartModule\r\n} from 'highcharts-angular';\r\nimport {\r\n  HttpClientModule\r\n} from '@angular\/common\/http';\r\n\r\n@NgModule({\r\n  declarations: [\r\n    AppComponent\r\n  ],\r\n  imports: [\r\n    BrowserModule,\r\n    AppRoutingModule,\r\n    HttpClientModule,\r\n    HighchartsChartModule\r\n  ],\r\n  providers: [],\r\n  bootstrap: [AppComponent]\r\n})\r\nexport class AppModule {}<\/pre>\n<p><b>Step 3<\/b><br \/>\nThe last step is to initialize the chart data and options by adding the <code>highcharts-chart<\/code> directive and some property binding into <code>app.component.html<\/code>:<\/p>\n<pre>&lt;highcharts-chart *ngIf=\"chartOptions\"\r\n  [Highcharts]=\"Highcharts\"\r\n  [options]=\"chartOptions\"\r\n  style=\"width: 100%; height: 400px; display: block;\"\r\n&gt;&lt;\/highcharts-chart&gt;<\/pre>\n<p>And here is the final result:<br \/>\nAs you can see on the GIF below, the line chart gets the WebSocket URL data once the connection is made. Whenever a new message comes from the server, WebSocketSubject will emit that message a value in the stream the chart\u2019s data is updated and displayed.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-20542\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2021\/01\/14132123\/ezgif.com-gif-maker.gif\" alt=\"Live data with RxJs WebSocket, HighCharts, and Angular\" width=\"900\" height=\"398\" \/><\/p>\n<p>Feel free to share your experience with Live data using RxJs WebSocket, HighCharts, and Angular in the comment section below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to create a live interactive chart with Live data with RxJs WebSocket, HighCharts, and AngularJS.<\/p>\n","protected":false},"author":249,"featured_media":21958,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"meta_title":"","meta_description":"","hc_selected_options":[],"footnotes":""},"categories":[1105],"tags":[822,1094,1031],"coauthors":[779],"class_list":["post-20527","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-integration","tag-angular","tag-highcharts-core","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/20527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/users\/249"}],"replies":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/comments?post=20527"}],"version-history":[{"count":1,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/20527\/revisions"}],"predecessor-version":[{"id":29336,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/20527\/revisions\/29336"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media\/21958"}],"wp:attachment":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media?parent=20527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/categories?post=20527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/tags?post=20527"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/coauthors?post=20527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}