cc_signalr 1.1.1+4 cc_signalr: ^1.1.1+4 copied to clipboard
CC SignalR is a Flutter plugin that provides SignalR support, enabling easy management
CC SignalR for Flutter #
CC SignalR is a plugin that provides SignalR support for Flutter applications. This plugin makes it easy to manage SignalR connections and communicate using an event-driven approach.
Installation #
Add the following dependency to your pubspec.yaml
file:
dependencies:
cc_signalr: ^1.0.0
Then, run the following command in the terminal to load the dependencies:
flutter pub get
Usage #
1. Define Connection Options #
Use the init
method to add the necessary modules. Define the connection options and configurations directly within this method.
import 'package:cc_signalr/cc_signalr.dart';
void main() {
CCSignalR.init(
connectionOptions: HttpConnectionOptions(
skipNegotiation: true,
transport: HttpTransportType.WebSockets,
logMessageContent: false,
),
signalROptions: CCSignalROptions(
url: "https://example.com/signalrHub",
autoReconnect: true,
hubProtocol: JsonHubProtocol(),
onConnected: (HubConnection connection) async {
print("Connected");
},
onDisconnected: (value) {
print("Disconnected");
},
onReconnected: (value) {
print("Reconnected");
},
),
loggingOptions: CCSignalRLogging(
logEnabled: true,
logLevel: Level.INFO,
),
modules: [
Example(),
],
);
}
2. Using Modules #
You can use your example module to receive and manage messages over SignalR.
void main() {
// Other initialization code...
// Start the connection
CCSignalR.connect();
// Subscribe to the module
CCSignalR.getModule<Example>().subscribe();
// Unsubscribe from the module
CCSignalR.getModule<Example>().unsubscribe();
}
3. Create Your Own Module #
The HUBModule
class is inherited to listen for a specific SignalR message. This class listens for messages coming over SignalR and prints these messages to the console. You can create your own module by defining a class like this:
import 'package:cc_signalr/src/modules/hub_module.dart';
class Example extends HUBModule {
Example() : super("receiveMainPageStream");
@override
void listen(List<Object?>? parameters) {
print("Broadcast : " + parameters.toString());
}
}
Example Code #
Below is the complete example code:
void main() {
CCSignalR.init(
connectionOptions: HttpConnectionOptions(
skipNegotiation: true,
transport: HttpTransportType.WebSockets,
logMessageContent: false,
),
signalROptions: CCSignalROptions(
url: "https://example.com/signalrHub",
autoReconnect: true,
hubProtocol: JsonHubProtocol(),
onConnected: (HubConnection connection) async {
String connectionId = await connection.invoke("getConnectionId") as String;
print("Connected : " + connectionId);
},
onDisconnected: (value) {
print("Disconnected");
},
onReconnected: (value) {
print("Reconnected");
},
),
loggingOptions: CCSignalRLogging(
logEnabled: true,
logLevel: Level.INFO,
),
modules: [
Example(),
],
);
CCSignalR.connect();
CCSignalR.getModule<Example>().subscribe();
}
Explanation
- Inheritance: The
Example
class is inherited from theHUBModule
class. This means that theExample
class inherits all the properties and methods of theHUBModule
class. - Constructor: The constructor of the
Example
class calls the constructor of theHUBModule
class with the"receiveMainPageStream"
parameter. This means that theExample
class will listen to the"receiveMainPageStream"
message. listen
Method: This method listens for messages coming from SignalR. Theparameters
parameter contains the content of the message from SignalR, and this content is printed to the console.
Contributing #
If you would like to contribute to this project, please send a pull request or open an issue.
License #
This project is licensed under the MIT License. For more information, see the LICENSE
file.