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.mzph.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