在Android应用程序中从Laravel Echo Socket IO后端接收消息

Laravel framework provides very nice way of subscribing to channels and listening to events. In this article we will explore how we can receive broadcasted events from Laravel web app, in android application, using WebSockets.

Laravel框架提供了一种很好的订阅频道和监听事件的方式。 在本文中,我们将探索如何使用WebSockets在Android应用程序中从Laravel Web应用程序接收广播事件。

Image for post
Laravel Echo Web & Android App Sample
Laravel Echo Web和Android应用程序示例

Article will be formed in two sections, first will describe Laravel web application setup and second section one will describe how we can integrate Socket IO in android application. If you are interested in android part of this article, you can skip web setup section. On the end of the article are the links to source code of both applications, so you can test the whole setup.

本文将分为两部分,第一部分将描述Laravel Web应用程序设置,第二部分将描述我们如何将Socket IO集成到Android应用程序中。 如果您对本文的android部分感兴趣,可以跳过Web设置部分。 本文末尾有两个应用程序的源代码链接,因此您可以测试整个设置。

Laravel Web应用程序设置 (Laravel Web Application Setup)

Main purpose of Laravel web application is to create/broadcast messages to available clients. In our web application we’ll use Redis broadcaster with Socket.IO server. In this section we’ll see all the steps required for Laravel web application to send broadcasted messages via WebSocket.

Laravel Web应用程序的主要目的是向可用客户端创建/广播消息。 在我们的Web应用程序中,我们将Redis广播器与Socket.IO服务器一起使用。 在本节中,我们将看到Laravel Web应用程序通过WebSocket发送广播消息所需的所有步骤。

The Redis broadcaster will broadcast messages from our MessagesController (that we’ll create later) using pub/sub feature. We’ll pair it with a Socket IO server so we can receive messages from Redis and broadcast them to Socket IO channels.

Redis广播者将使用发布/订阅功能广播来自我们的MessagesController (稍后将创建)的消息 。 我们将其与Socket IO服务器配对,以便我们可以接收来自Redis的消息并将其广播到Socket IO通道。

So, the very first thing is to install Laravel Echo:

因此,第一件事就是安装Laravel Echo:

npm install --save laravel-echo

and Redis, via composer:

和Redis,通过作曲家:

composer require predis/predis

Don’t forget to set Redis as broadcast driver in .env file:

不要忘记在.env文件中将Redis设置为广播驱动程序:

BROADCAST_DRIVER=redis

And specify Redis properties:

并指定Redis属性:

REDIS_CLIENT=predis
REDIS_PREFIX=""

In the next step, we are going to pair Redis broadcaster with Socket.IO server. Because we want to receive send messages in web application (and also in android application), we’ll need to install appropriate Javascript library:

在下一步中,我们将Redis广播器与Socket.IO服务器配对。 因为我们要在Web应用程序(以及android应用程序)中接收发送消息,所以我们需要安装适当的Javascript库:

npm install — save socket.io-client

After installing Socket.IO Javascript client, we need to instantiate global Echo object with Socket.IO as broadcaster (in resources/js/bootstrap.js).

安装Socket.IO Javascript客户端后,我们需要使用Socket.IO作为广播器实例化全局Echo对象(在resources / js / bootstrap.js中 )。

import Echo from 'laravel-echo';


window.io = require('socket.io-client');


window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001'
});

After specifying Echo object, we need to execute the following command in order to include it in our app.js, that we are going to use later in article, for listening of Echo events in our web app:

在指定Echo对象之后,我们需要执行以下命令,以将其包含在我们的app.js中 ,我们将在本文后面使用它,以监听Web应用程序中的Echo事件:

npm install
npm run dev

Now, we are very close to the finishing of Laravel dependencies configuration process. The very last thing is to install Socket.IO server. Because Laravel doesn’t get with Socket.IO server included, we’ll use community driven Socket.IO server: https://github.com/tlaverdure/laravel-echo-server

现在,我们非常接近Laravel依赖项配置过程的完成。 最后一件事是安装Socket.IO服务器。 由于Lara

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值