Category: Laravel, PHP, javascript

In this article we will explore how we can receive broadcasted events from Laravel web app, in android application, using WebSockets. 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.

Route to MessagesController, specified in web.php: Now is the time for starting our web application: and by browsing the following url, we can see our messages form: Here we are finished with simple Laravel Application, so we can start with Android application implementation.

Main screen will have simple layout, only one TextView that will show all received messages: In order to make socket connection to our Laravel Echo server, we need to import appropriate library in android project: This library will allow us to connect to the socket server and to receive messages, sent from the server.

In order to parse received message, we are going to use Gson library, so we need to import it in our app/build.gradle file: We also need to add appropriate repository location from which this library will be fetched, in project level build.gradle file: After we have created BaseSocketActivity, we can set our MainActivity to inherit BaseSocketActivity and call appropriate methods for establishing and destroying the socket connection: Since cleartext HTTP traffic is not explicitly permitted, we need to specify that in network security configuration (res/xml/network_security_config.xml file): and include this file as an application tag property of our manifest, like: Finally, don’t forget to set internet permission in manifest: Now you can test everything in android emulator.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners