This document provides instructions for setting up a Node.js backend server that integrates with Pusher to send real-time notifications to a Flutter application. The backend is capable of broadcasting messages to any subscribed clients via Pusher Channels.
- A Pusher account with an app created on the Pusher Dashboard.
- Node.js and npm installed on your server.
- A Flutter app with Pusher client integration.
-
Clone or download the backend server code from the repository:
git clone https://github.com/monirhossain09544/pusher-backend.git
-
Navigate to the root directory of the backend server code in your terminal.
-
Run
npm install
to install all required dependencies.
Set up your Pusher instance in the server.js
:
const Pusher = require('pusher');
const pusher = new Pusher({
appId: "YOUR_PUSHER_APP_ID",
key: "YOUR_PUSHER_APP_KEY",
secret: "YOUR_PUSHER_APP_SECRET",
cluster: "YOUR_PUSHER_APP_CLUSTER",
useTLS: true
});
Running the Server Start the server by running the following command in your terminal:
node server.js
The server will then listen for incoming HTTP POST requests to trigger notifications.
To send a notification, make a POST request to the /notify endpoint with a JSON payload containing the message. This can be done using Postman or via curl:
curl -X POST -H "Content-Type: application/json" -d "{\"message\":\"Your notification message\"}" http://<YOUR_SERVER_URL>:3000/notify
Be sure to replace <YOUR_SERVER_URL> with the actual URL where your server is hosted.
In your Flutter application, set up subscription to the Pusher channel to listen for messages as shown in the example below:
void initPusher() async {
await Pusher.init("YOUR_PUSHER_APP_KEY", PusherOptions(cluster: "YOUR_PUSHER_APP_CLUSTER"));
Pusher.connect();
Channel channel = await Pusher.subscribe("notification-channel");
channel.bind("new-notification", (PusherEvent event) {
// Handle the notification
print(event.data);
});
}
Refer to the pusher_websocket_flutter package documentation for detailed integration steps.
When preparing for production, make sure to:
- Use environment variables to store your sensitive keys and secrets. Implement authentication and authorization mechanisms to protect your
- endpoints. Use SSL/TLS encryption for securing data in transit.
- Support For further assistance, refer to the Pusher Channels
documentation or contact Pusher support.