ios pusher使用_如何使用JavaScript和Pusher構建實時圖

ios pusher使用

by Rahat Khanna

通過拉哈特·漢娜

如何使用JavaScript和Pusher構建實時圖 (How to build a Realtime Graph using JavaScript and Pusher)

The world needs everything uber-fast now. There are plenty of data streams being generated by different systems every day. They serve in making decisions in many industries. Realtime monitoring and analysis have become very important today. Data streams include realtime monitoring of website traffic, server performance, weather updates, and IOT sensors. It is important to analyze and interpret this burst of data, for which interactive charts and graphs are an excellent solution.

現在,世界需要一切都更快。 每天,不同的系統都會生成大量的數據流。 他們在許多行業中做出決策。 如今,實時監控和分析已變得非常重要。 數據流包括對網站流量,服務器性能,天氣更新和IOT傳感器的實時監控。 重要的是分析和解釋這些數據,對于交互式圖表來說,這是一個很好的解決方案。

In this article, we will be building a Node.js Server to expose APIs to provide historical data for a metric (in this case, weather in London). It will also provides an API to ingest new data points. We will also be building a front-end app with a Line Chart to display the temperature changes in London weather in realtime. The application we build will look something like this:

在本文中,我們將構建一個Node.js服務器以公開API,以提供度量的歷史數據(在本例中為倫敦的天氣)。 它還將提供一個API以提取新的數據點。 我們還將構建一個帶有折線圖的前端應用程序,以實時顯示倫敦天氣的溫度變化。 我們構建的應用程序將如下所示:

注冊Pusher (Signup for Pusher)

The first step to start this tutorial is to Signup at Pusher or login with your existing credentials if you already have an account. After logging in, you will need to create a new app and select Vanilla JavaScript for the front-end along with Node.js for the back-end. You will then be brought to a landing page containing the ‘getting started’ code for both front-end and back-end, which we will use later on in the tutorial.

開始本教程的第一步是在Pusher進行注冊,或者如果您已經有一個帳戶,則使用您現有的憑據登錄。 登錄后,您將需要創建一個新應用,并選擇Vanilla JavaScript作為前端以及Node.js作為后端。 然后,您將被帶到一個包含前端和后端“入門”代碼的登錄頁面,我們將在本教程的后面部分中使用該代碼。

用于監視和分析系統的Node.js服務器API (Node.js Server APIs for Monitoring and Analytics System)

The essential APIs for any analytics systems for any metric or entity are:

對于任何度量標準或實體的任何分析系統,必不可少的API是:

  1. Ingestion API — An API to ingest the new data points for any particular entity. In our server for this blog post, we will make an API to ingest new temperature data at a particular time for London. This API can be called by any global weather system or any IOT sensor.

    提取API-提取任何特定實體的新數據點的API。 在此博客文章的服務器中,我們將創建一個API以在特定時間獲取倫敦的新溫度數據。 任何全球天氣系統或任何IOT傳感器均可調用此API。
  2. Historical Data API — This API will return all the data within a range from this date in time. For our server, we will create a simple API. It will return some static historical data with limited data points for London’s temperature values for any day.

    歷史數據API-此API將返回該日期范圍內的所有數據。 對于我們的服務器,我們將創建一個簡單的API。 它將返回某些靜態歷史數據,其中任意一天的倫敦溫度值的數據點均有限。

Node.js Express服務器框架 (Node.js Express Server Skeleton)

We will create a basic Express Server along with instantiating the Pusher library server instance. We will create a new folder for our project and create a new file server.js. Add the following code to this file:

我們將創建一個基本的Express Server并實例化Pusher庫服務器實例。 我們將為我們的項目創建一個新文件夾,并創建一個新文件server.js 。 將以下代碼添加到該文件:

獲取歷史溫度數據的API (API to Get Historical Temperature Data)

Now, we will add some static data regarding London’s temperature at certain times during a day and store it in any JavaScript variable. We will also expose a route to return this data whenever someone invokes it using a GET HTTP call.

現在,我們將添加有關一天中某些時間倫敦溫度的靜態數據,并將其存儲在任何JavaScript變量中。 每當有人使用GET HTTP調用調用此數據時,我們還將公開一條返回此數據的路由。

提取溫度數據點的API (API to Ingest Temperature Data Point)

Now we will add the code for exposing an API to ingest the temperature at a particular time. We will expose a GET HTTP API with temperature and time as query parameters. We will validate that they are not empty parameters. We store them by pushing in the dataPoints array of our static JavaScript variable londonTempData. Please add the following code to the server.js file

現在,我們將添加用于暴露API以在特定時間攝取溫度的代碼。 我們將公開一個以溫度和時間為查詢參數的GET HTTP API。 我們將驗證它們不是空參數。 我們通過將其放入靜態JavaScript變量londonTempDatadataPoints數組中來存儲它們。 請將以下代碼添加到server.js文件中

In the above code, apart from storing in the data source, we will also trigger an event ‘new-temperature’ on a new channel ‘london-temp-chart’. For every unique data source or a chart, you can create a new channel.

在上面的代碼中,除了存儲在數據源中之外,我們還將在新通道“ london-temp-chart”上觸發事件“ new-temperature 。 對于每個唯一的數據源或圖表,您可以創建一個新通道。

The event triggered by our server will be processed by the front-end to update the chart/graph in realtime. The event can contain all the important data which the chart needs to display the data point correctly. In our case, we will be sending the temperature at the new time to our front-end.

由我們的服務器觸發的事件將由前端處理,以實時更新圖表。 該事件可以包含圖表正確顯示數據點所需的所有重要數據。 在我們的情況下,我們將在新時間將溫度發送到前端。

使用Vanilla JavaScript和Chart.js構建前端應用程序 (Building the Front-End App using Vanilla JavaScript and Chart.js)

Now, we will build the front-end application. It will display a Line Chart representing the changes in temperature for London City at different times throughout the day. The key approach for displaying realtime graphs is:

現在,我們將構建前端應用程序。 它將顯示一個折線圖,該折線圖表示倫敦全天在不同時間的溫度變化。 顯示實時圖形的關鍵方法是:

  1. We have to make an initial Ajax call to fetch historical data and render the graph with the existing data.

    我們必須進行初始Ajax調用以獲取歷史數據并使用現有數據呈現圖形。
  2. We will subscribe to any events for new data points being stored on a particular channel.

    我們將為存儲在特定通道中的新數據點訂閱任何事件。

基本HTML模板 (Basic HTML Template)

We will create a new folder called public in our project root and then create a new file index.html in this folder. This file will contain the basic HTML code to render a simple header and a sub-header with the app name along with few icons. We will also import the Pusher JavaScript library from its CDN URL.

我們將在項目根目錄中創建一個名為public的新文件夾,然后在此文件夾中創建一個新文件index.html 。 該文件將包含基本HTML代碼,以呈現一個簡單的標題和帶有應用名稱以及少量圖標的子標題。 我們還將從其CDN URL導入Pusher JavaScript庫。

添加圖表庫 (Adding Charts Library)

In JavaScript and HTML apps, we have to use either SVG or Canvas to build graphical components to represent mathematical graphs. There are numerous open source libraries that can help you render different chart types. These include Bar Charts, Pie Charts, Line Charts and Scatter Charts.

在JavaScript和HTML應用程序中,我們必須使用SVG或Canvas來構建表示數學圖形的圖形組件。 有許多開源庫可以幫助您呈現不同的圖表類型。 這些包括條形圖,餅圖,折線圖和散點圖。

For our project, we will choose Chart.js as it has fairly simple API and renders robust charts using a Canvas HTML tag. You can choose any charting library but keep in mind that the library should have a means to update the chart without completely re-rendering it. Chart.js provides a method on any instantiated chart to update it.

對于我們的項目,我們將選擇Chart.js,因為它具有相當簡單的API,并使用Canvas HTML標簽呈現了強大的圖表。 您可以選擇任何圖表庫,但請記住,該庫應具有一種在不完全重新呈現圖表的情況下更新圖表的方法。 Chart.js在任何實例化圖表上都提供了一種方法來對其進行更新。

Add the following code to your index.html file at appropriate places

將以下代碼添加到index.html文件中的適當位置

添加JavaScript文件并實例化Pusher客戶端庫 (Adding JavaScript File and Instantiating Pusher client-side library)

Now we will create a new file app.js in our public folder and also add the following code to instantiate the Pusher client-side library.

現在,我們將在公共文件夾中創建一個新文件app.js ,并添加以下代碼以實例化Pusher客戶端庫。

In the above code, we have also added few utility methods to make an Ajax call and also show or hide elements from the DOM API.

在上面的代碼中,我們還添加了一些實用程序方法來進行Ajax調用,并顯示或隱藏DOM API中的元素。

添加代碼以獲取歷史數據 (Adding Code to fetch Historical Data)

Now, we will add the code to fetch the historical temperature data to display the graph with the initial values. We will also instantiate a new Chart object with a specific config to render a Line Chart. You can read more about how to construct these configs at the Chart.js documentation.

現在,我們將添加代碼以獲取歷史溫度數據以顯示帶有初始值的圖形。 我們還將實例化具有特定配置的新Chart對象,以呈現折線圖。 您可以在Chart.js文檔中閱讀有關如何構造這些配置的更多信息 。

Please add the following code to the app.js file:

請將以下代碼添加到app.js文件中:

In the above code, we have added a function named renderWeatherChart. This will be used to render the chart using latest data which is embedded in the chartConfig variable under the key datasets. If we want to draw multiple line charts on the same canvas, we can add more elements to this array.

在上面的代碼中,我們添加了一個名為renderWeatherChart的函數 這將用于使用嵌入在關鍵數據集下的chartConfig變量中的最新數據來呈現圖表。 如果要在同一畫布上繪制多個折線圖,則可以向此數組添加更多元素。

The data key in each of the elements of the array will display the different points on the graph. We will make an ajax request to the /getTemperature api to fetch all the data points and put them into this key. We will call the rendering method to display the graph then. Now we can run the command node server.js and then go to the browser with the following URL to see the initial chart rendered using the data.

數組每個元素中的數據鍵將在圖形上顯示不同的點。 我們將向/ getTemperature api發出ajax請求,以獲取所有數據點并將它們放入此鍵中。 然后,我們將調用render方法來顯示圖形。 現在,我們可以運行命令node server.js ,然后使用以下URL進入瀏覽器以查看使用數據呈現的初始圖表。

http://localhost:9000/

In order to style our app properly, please add the following CSS into a new style.css file inside the public folder. Add the following code to that file:

為了正確設置我們的應用程序樣式,請在公共文件夾內的新style.css文件中添加以下CSS。 將以下代碼添加到該文件:

收到新事件后更新圖表的代碼 (Code to Update Graph on new event received)

Now we want to subscribe to the unique channel on which our server will be sending update events for this graph. For our project, the channel is named london-temp-chart and the event is named new-temperature. Please add the following code to process the event and then update the chart in realtime:

現在,我們要訂閱服務器將在其上發送此圖的更新事件的唯一通道。 對于我們的項目,該通道名為london-temp-chart ,事件名為new-temperature 。 請添加以下代碼以處理事件,然后實時更新圖表:

In order to see this code in action, you have to refresh the browser and you will see the initial chart. Now we have to ingest a new data point. You would need to call the following API either by using some mock API calling tool or using the following URL with different values in the browser.

為了查看此代碼的運行情況,您必須刷新瀏覽器,然后才能看到初始圖表。 現在我們必須攝取一個新的數據點。 您可能需要通過使用某些模擬API調用工具或在瀏覽器中使用具有不同值的以下URL來調用以下API。

http://localhost:9000/addTemperature?temperature=17&time=1500

In order to test your chart update code, you can use the following temporary code in your app.js file. It will make dummy Ajax requests to the above URL after a specific time interval.

為了測試圖表更新代碼,您可以在app.js文件中使用以下臨時代碼。 在特定時間間隔后,它將向上述URL發出虛擬Ajax請求。

Here is the GitHub repo for reference to the complete code.

這是GitHub存儲庫,供完整代碼參考。

結論 (Conclusion)

Finally, our realtime analytics app is ready. We will see the weather temperature chart for London city updating in realtime.

最后,我們的實時分析應用已準備就緒。 我們將實時查看倫敦市的氣溫圖表。

We can use the code from this blog post for any charts library. It can also render any type of chart like Bar Chart, Scatter Chart or Pie Chart to update in realtime.

我們可以將此博客文章中的代碼用于任何圖表庫。 它還可以呈現任何類型的圖表,例如條形圖,散點圖或餅圖以進行實時更新。

This code can also be used in multiple Enterprise Apps. For example, monitoring dashboards, analytics reports, sensor regulatory apps, and financial apps. The Pusher library helps us send realtime events to all connected client-side apps. These apps can consume the data to update the charts in realtime.

此代碼也可以在多個企業應用程序中使用。 例如,監視儀表板,分析報告,傳感器監管應用程序和財務應用程序。 Pusher庫可幫助我們將實時事件發送到所有連接的客戶端應用程序。 這些應用程序可以使用數據來實時更新圖表。

This article was originally published on Pusher’s blog.

本文最初發布在Pusher的博客上。

翻譯自: https://www.freecodecamp.org/news/how-to-build-a-real-time-graph-using-javascript-pusher-d15ccb7a4b82/

ios pusher使用

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/394932.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394932.shtml
英文地址,請注明出處:http://en.pswp.cn/news/394932.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python數據分析與基礎實戰_《python數據分析與挖掘實戰》基礎概念

數據建模.png 數據挖掘的基本任務:利用分類與預測、聚類分析、關聯規則、時序模式、偏差檢測、智能推薦等方法,幫助企業提取數據中蘊含的商業價值,提高企業競爭力。 數據探索:異常值分析、缺失值分析、相關分析和周期性分析。 數據預處理:數據…

簡述JAVA線程調度的原理,Rxjava原理(二)--線程調度

1. 創建線程池和線程管理策略分析// 在開發中使用Rxjava來完成線程切換會調用到以下方法(還有幾個就不一一列舉了,原理一樣的),那么就從這里開始分析Schedulers.io()Schedulers.computation()Schedulers.newThread()AndroidSchedulers.mainThread()當我們…

[前端隨筆][css] 彈性布局

說在前面 彈性布局&#xff0c;顧名思義就是有彈性&#xff0c;能夠根據屏幕/當前空間大小自由伸縮的。使用彈性布局可以很好的適應各種尺寸的客戶端。 關鍵代碼 display:flex;    設定元素為彈性布局  <文檔傳送門> box-flex: 參數;   設定元素為彈性布局  &…

不同的模塊中定義同樣的宏為不同的值合法嗎_如何創建自定義的建模規范

本文摘要&#xff1a;主要介紹如何創建自定義的建模規范檢查&#xff0c;以及在建模規范檢查中&#xff0c;如何增加自動修正模型使之符合規范。比如我們想創建一個自定義的規則&#xff0c;對于constant模塊&#xff0c;1. 如果value是參數的話&#xff0c;則輸出數據類型必須…

DBCP連接池配置常用參數說明

參數默認值說明username\傳遞給JDBC驅動的用于建立連接的用戶名password\傳遞給JDBC驅動的用于建立連接的密碼url\傳遞給JDBC驅動的用于建立連接的URLdriverClassName\使用的JDBC驅動的完整有效的Java 類名initialSize 0初始化連接:連接池啟動時創建的初始化連接數量,1.2版本后…

科大訊飛 ai算法挑戰賽_為井字游戲挑戰構建AI算法

科大訊飛 ai算法挑戰賽by Ben Carp通過本卡爾普 為井字游戲挑戰構建AI算法 (Building an AI algorithm for the Tic-Tac-Toe challenge) As part of the freeCodeCamp curriculum, I was challenged build a Tic-Tac-Toe web app. It was a real pleasure.作為freeCodeCamp課程…

js serialize php 解,[轉]JavaScript 版本的 PHP serialize/unserialize 完整實現

下載: phpserializer.js/* phpserializer.js - JavaScript to PHP serialize / unserialize class.** This class is designed to convert php variables to javascript* and javascript variables to php with a php serialize unserialize* compatible way.** Copyright (C) …

Git 的 .gitignore 配置

.gitignore 配置文件用于配置不需要加入版本管理的文件&#xff0c;配置好該文件可以為我們的版本管理帶來很大的便利&#xff0c;以下是個人對于配置 .gitignore 的一些心得。 1、配置語法&#xff1a; 以斜杠“/”開頭表示目錄&#xff1b; 以星號“*”通配多個字符&#xff…

wsdl文件是怎么生成的_C++ 動態庫.dll的生成---超級詳細!!!

怎么將建好的工程生成.dll工程&#xff1f;1、在C中打開工程2、運行結果&#xff1a;輸出Print修改開始&#xff1a;1、打開屬性。2、修改以下內容&#xff1a;目標文件擴展名&#xff0c;由.exe--》.dll,直接刪除修改即可配置類型&#xff0c;由.exe--》.dll,下拉菜單可選擇最…

時鐘設置

date --set"05/31/16 18:16" 時鐘設置 設置系統時間# date --set“07/07/06 10:19" &#xff08;月/日/年 時:分:秒&#xff09;2、hwclock/clock查看硬件時# hwclock --show# clock --show設置硬件時間# hwclock --set --date"07/07/06 10:19" &…

《成為一名機器學習工程師》_成為機器學習的拉斐爾·納達爾

《成為一名機器學習工程師》by Sudharsan Asaithambi通過Sudharsan Asaithambi 成為機器學習的拉斐爾納達爾 (Become the Rafael Nadal of Machine Learning) One year back, I was a newbie to the world of Machine Learning. I used to get overwhelmed by small decisions…

HTTP基本認證(Basic Authentication)的JAVA示例

大家在登錄網站的時候&#xff0c;大部分時候是通過一個表單提交登錄信息。但是有時候瀏覽器會彈出一個登錄驗證的對話框&#xff0c;如下圖&#xff0c;這就是使用HTTP基本認證。下面來看看一看這個認證的工作過程:第一步: 客戶端發送http request 給服務器,服務器驗證該用戶…

php-fpm 內存 facebook,【百家號】臉書百科,安裝php-fpm-5.4.16-42.遇到的小問題 Web程序 - 貪吃蛇學院-專業IT技術平臺...

環境&#xff1a;redhat 7.2版本 yum源也是7.2的iso[[email protected] lnmp_soft]# yum -y install php-fpm-5.4.16-42.el7.x86_64.rpm已加載插件&#xff1a;langpacks, product-id, search-disabled-repos, subscription-managerThis system is not registered to Red Hat S…

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)

昨晚的沒來得及打&#xff0c;最近錯過好幾場CF了&#xff0c;這場應該不算太難 A. Unimodal Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputArray of integers is unimodal, if: it is strictly increasing in…

python能print中文嗎_python怎么print漢字

今天就為大家分享一篇python中使用print輸出中文的方法&#xff0c;具有很好的參考價值&#xff0c;希望對大家有所幫助。看Python簡明教程&#xff0c;學習使用print打印字符串&#xff0c;試了下打印中文&#xff0c;不行。&#xff08;推薦學習&#xff1a;Python視頻教程&a…

ajax的一些相關

1、AJAX Asynchronous&#xff08;異步的&#xff09; JavaScript and XML AJAX是能不刷新整個網頁的前提下&#xff0c;更新內容。通過少量的數據交換&#xff0c;達成局部頁面刷新的效果。 而form表單提交經常是刷新整個頁面&#xff0c;很繁瑣 2、AJAX是基于現有的Internet…

select ...as_一起使用.select .map和.reduce方法可充分利用Ruby

select ...asby Declan Meehan由Declan Meehan 一起使用.select .map和.reduce方法可充分利用Ruby (Get the most out of Ruby by using the .select .map and .reduce methods together) You should absolutely never ever repeat yourself when writing code. In other word…

一些書單

僅對近來的學習做些回顧吧 學習永無止境--> 2015年已完成書單&#xff1a; 文學&#xff1a; 硅谷之火浪潮之巔天才在左瘋子在右從0到1生命咖啡館黑客與畫家奇思妙想&#xff1a;15位計算機天才及其重大發現喬布斯傳平凡的世界&#xff08;三部全&#xff09;一只iphone的全…

oracle 11gogg,【OGG】Oracle GoldenGate 11g (二) GoldenGate 11g 單向同步配置 上

Oracle GoldenGate 11g (二)GoldenGate 11g 單向同步配置 上ItemSource SystemTarget SystemPlatformRHEL6.4 - 64bitRHEL6.4 - 64bitHostnamerhel64.oracle.comora11g.oracle.comDatabaseOracle 11.2.0.3Oracle 11.2.0.3Character SetAL32UTF8AL32UTF8ORACLE_SIDPRODEMREPList…

今天聽說了一個壓縮解壓整型的方式-group-varint

group varint https://github.com/facebook/folly/blob/master/folly/docs/GroupVarint.md 這個是facebook的實現 https://www.slideshare.net/parallellabs/building-software-systems-at-google-and-lessons-learned/48-Group_Varint_Encoding_Idea_encode