Home  >  Article  >  Backend Development  >  How to implement message push in php

How to implement message push in php

(*-*)浩
(*-*)浩Original
2019-11-13 15:16:445451browse

How to implement message push in php

How to develop the message push function when we encounter it?

1. Ajax polling, requesting server data regularly (Recommended learning: PHP video tutorial)

Passed Observing that thinkphp official website seems to also use this method, this method is organized below:

Notify.php
//获取通知消息
    public function getNotifyCount()
    {
        $msg = db('message_logs')->where('isscan',0)->count();
        RestfulTools::restData($msg); //这里是封装好的json_encode方法
    }
notify.js
<span class="am-icon-envelope-o"></span> 消息 <span class="am-badge am-badge-warning" id="msgCount"> 
{$msgCount}  //这是是通过tp的 assign方法分配过来的变量,作为初始值
</span>

<script type="text/javascript">

    var getting = {
        url:"{:url(&#39;Notify/getNotifyCount&#39;)}",
        dataType:&#39;json&#39;,
        success:function(res) {
            console.log(res);
            var msgCount = res.result;
            $("#msgCount").html(msgCount); //用js的 html方法去改变id为msgCount的值
        }
    };

    //Ajax定时访问服务端,这里是3分钟请求一次。

    window.setInterval(function(){
        $.ajax(getting)
    },180000);

</script>

The above is the detailed content of How to implement message push in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn