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.
Add the following dependency to your pubspec.yaml file:
dependencies:
cc_signalr: ^1.0.0Then, run the following command in the terminal to load the dependencies:
flutter pub getUse 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(),
],
);
}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();
}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());
}
}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();
}- Inheritance: The
Exampleclass is inherited from theHUBModuleclass. This means that theExampleclass inherits all the properties and methods of theHUBModuleclass. - Constructor: The constructor of the
Exampleclass calls the constructor of theHUBModuleclass with the"receiveMainPageStream"parameter. This means that theExampleclass will listen to the"receiveMainPageStream"message. listenMethod: This method listens for messages coming from SignalR. Theparametersparameter contains the content of the message from SignalR, and this content is printed to the console.
If you would like to contribute to this project, please send a pull request or open an issue.
This project is licensed under the MIT License. For more information, see the LICENSE file.