सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
एक फ्लटर प्लगइन जो पुशर चैनल्स के एकीकरण के माध्यम से मोबाइल ऐप में रियल-टाइम संचार की अनुमति देता है। यह पुशर-वेबसोकेट-जावा और पुशर-वेबसोकेट-स्विफ्ट के नेटिव व्रैपर्स के माध्यम से एंड्रॉइड और आईओएस का समर्थन करता है, जिससे डेवलपर्स लाइव अपडेट्स प्राप्त कर सकते हैं, इवेंट्स को संभाल सकते हैं और चैनल्स को कुशलता से प्रबंधित कर सकते हैं। चैट ऐप्स, लाइव नोटिफिकेशन्स और कम लेटेंसी डेटा सिंक्रनाइजेशन की आवश्यकता वाले सहयोगात्मक टूल्स के लिए आदर्श है।
उपयोग
रियल-टाइम चैट एप्लिकेशन्स
लाइव स्थिति अपडेट
सहयोगात्मक संपादन उपकरण
लाइव डेटा के साथ पुश नोटिफिकेशन
आईओटी डिवाइस मॉनिटरिंग
मुख्य विशेषताएँ
क्रॉस-प्लेटफॉर्म समर्थन (Android और iOS)
कम लेटेंसी इवेंट हैंडलिंग
चैनल और उपस्थिति प्रबंधन
सुरक्षित WebSocket संयोजन
Pusher डैशबोर्ड के साथ आसान एकीकरण
इनके लिए उपयुक्त
रियल-टाइम फीचर्स बना रहे डेवलपर्स
तात्कालिक डेटा सिंक की आवश्यकता वाले ऐप्स
Pusher Channels बैकएंड का उपयोग करने वाली टीमें
लाइव अपडेट की आवश्यकता वाले मोबाइल ऐप्स
विचार योग्य बातें
एक Pusher खाता और ऐप कुंजी की आवश्यकता होती है
Android और iOS प्लेटफॉर्म तक सीमित
बाह्य नेटिव पुस्तकालयों पर निर्भर करता है
नेटवर्क समस्याओं के लिए उचित त्रुटि संहति की आवश्यकता है
निजी चैनल्स के लिए प्रमाणीकरण सेटअप आवश्यक है
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
Set the minimum deployment target in the Podfile to 9.0. Go to ios/Podfile, then uncomment this line:
# platform :ios, '8.0'
Change it to:
platform :ios, '9.0'
You may have an issue subscribing to private channels if you're using a local pusher server like laravel-websockets, to fix this go to ios/Runner/Info.plist and add:
-keep class com.github.chinloyal.pusher_client.** { *; }
API Overview
Here's the API in a nutshell.
PusherOptions options = PusherOptions(
host: 'example.com',
wsPort: 6001,
encrypted: false,
auth: PusherAuth(
'http://example.com/auth',
headers: {
'Authorization': 'Bearer $token',
},
),
);
PusherClient pusher = PusherClient(
YOUR_APP_KEY,
options,
autoConnect: false
);
// connect at a later time than at instantiation.
pusher.connect();
pusher.onConnectionStateChange((state) {
print("previousState: ${state.previousState}, currentState: ${state.currentState}");
});
pusher.onConnectionError((error) {
print("error: ${error.message}");
});
// Subscribe to a private channelChannel channel = pusher.subscribe("private-orders");
// Bind to listen for events called "order-status-updated" sent to "private-orders" channel
channel.bind("order-status-updated", (PusherEvent event) {
print(event.data);
});
// Unsubscribe from channel
pusher.unsubscribe("private-orders");
// Disconnect from pusher service
pusher.disconnect();
More information in reference format can be found below.
The Pusher Constructor
The constructor takes an application key which you can get from the app's API Access section in the Pusher Channels dashboard, and a pusher options object.
If you are going to use private, presence or encrypted channels then you will need to provide a PusherAuth to be used when authenticating subscriptions. In order to do this you need to pass in a PusherOptions object which has had an auth set.
PusherAuth auth = PusherAuth(
// for auth endpoint use full url'http://example.com/auth',
headers: {
'Authorization': 'Bearer $token',
},
);
PusherOptions options = PusherOptions(
auth: auth
);
PusherClient pusher = PusherClient(YOUR_APP_KEY, options);
If auto connect is disabled then you can manually connect using connect() on the pusher instance.
Pusher Options Config
Most of the functionality of this plugin is configured through the PusherOptions object. You configure it by setting parameters on the object before passing it to the Pusher client. Below is a table containing all of the properties you can set.
Method
Parameter
Description
encrypted
bool
Whether the connection should be made with TLS or not.
auth
PusherAuth
Sets the authorization options to be used when authenticating private, private-encrypted and presence channels.
host
String
The host to which connections will be made.
wsPort
int
The port to which unencrypted connections will be made. Automatically set correctly.
wssPort
int
The port to which encrypted connections will be made. Automatically set correctly.
cluster
String
Sets the cluster the client will connect to, thereby setting the Host and Port correctly.
activityTimeout
int
The number of milliseconds of inactivity at which a "ping" will be triggered to check the connection. The default value is 120,000.
pongTimeout
int
The number of milliseconds the client waits to receive a "pong" response from the server before disconnecting. The default value is 30,000.
maxReconnectionAttempts
int
Number of reconnection attempts that will be made when pusher.connect() is called, after which the client will give up.
maxReconnectGapInSeconds
int
The delay in two reconnection extends exponentially (1, 2, 4, .. seconds) This property sets the maximum inbetween two reconnection attempts.
Reconnecting
The connect() method is also used to re-connect in case the connection has been lost, for example if a device loses reception. Note that the state of channel subscriptions and event bindings will be preserved while disconnected and re-negotiated with the server once a connection is re-established.
Disconnecting
pusher.disconnect();
After disconnection the PusherClient instance will release any internally allocated resources (threads and network connections)
Subscribing To Channels
Channels use the concept of channels as a way of subscribing to data. They are identified and subscribed to by a simple name. Events are bound to a channel and are also identified by name.
As mentioned above, channel subscriptions need only be registered once by the PusherClient instance. They are preserved across disconnection and re-established with the server on reconnect. They should NOT be re-registered. They may, however, be registered with a PusherClient instance before the first call to connect - they will be completed with the server as soon as a connection becomes available.
Public Channels
The default method for subscribing to a channel involves invoking the subscribe method of your client object:
Channel channel = pusher.subscribe("my-channel");
This returns a Channel object, which events can be bound to.
Private Channels
Private channels are created in exactly the same way as public channels, except that they reside in the 'private-' namespace. This means prefixing the channel name:
Subscribing to private channels involves the client being authenticated. See The Pusher Constructor section for the authenticated channel example for more information.
Private Encrypted Channels
Similar to Private channels, you can also subscribe to a private encrypted channel. This plugin fully supports end-to-end encryption. This means that only you and your connected clients will be able to read your messages. Pusher cannot decrypt them. These channels must be prefixed with 'private-encrypted-'
The name of the channel that the event was triggered on. (Optional)
data
String
The data that was passed to trigger, encoded as a string. If you passed an object then that will have been serialized to a JSON string which you can parse as necessary. (Optional)
userId
String
The ID of the user who triggered the event. This is only available for client events triggered on presence channels. (Optional)
Unbind Channel Events
You can unbind from an event by doing:
channel.unbind("order-status-updated");
Triggering Client Events
Once a private or presence subscription has been authorized and the subscription has succeeded, it is possible to trigger events on those channels.
Events triggered by clients are called client events. Because they are being triggered from a client which may not be trusted there are a number of enforced rules when using them. Some of these rules include:
Event names must have a 'client-' prefix
Rate limits
You can only trigger an event when the subscription has succeeded
Once connected you can access a unique identifier for the current client's connection. This is known as the socket Id. You can access the value once the connection has been established as follows:
String socketId = pusher.getSocketId();
For more information on how and why there is a socket Id see the documentation on authenticating users and excluding recipients.
Resolve Common Issues
iOS doesn't log when enableLogging is set to true
iOS logging doesn't seem to output to flutter console, however if you run the
app from Xcode you should be able to see the logs.
Subscribing to private channels with iOS
If using a local pusher server but are unable to subscribe to a private channel
then add this to your ios/Runner/Info.plist: