WebSockets are a powerful feature in Flutter, enabling real-time communication between a client and a server. They are commonly used in applications where you need instant updates or bidirectional communication. Here are some real-time applications of WebSockets in Flutter along with examples:
Real-time Chat Applications
Real-time Notifications
Collaborative Applications (e.g., Real-time Document Editing)
Online Gaming
Real-time Dashboards and Analytics
IoT Device Control and Monitoring
Real-time Chat Applications
Example: Building a chat application where messages sent by users are instantly reflected to other participants.
Implementation Steps:
- Connect to a WebSocket server.
- Listen for incoming messages and display them in the chat UI.
- Send messages through the WebSocket connection .
import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ChatScreen(),
);
}
}
class ChatScreen extends StatefulWidget {
@override
_ChatScreenState createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
final TextEditingController _controller = TextEditingController();
final _channel = WebSocketChannel.connect(
Uri.parse('ws://echo.websocket.org'),
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('WebSocket Chat'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Expanded(
child: StreamBuilder(
stream: _channel.stream,
builder: (context, snapshot) {
return ListView(
children: <Widget>[
Text(snapshot.hasData ? '${snapshot.data}' : ''),
],
);
},
),
),
TextField(
controller: _controller,
decoration: InputDecoration(
labelText: 'Send a message',
),
onSubmitted: (text) {
_channel.sink.add(text);
_controller.clear();
},
),
],
),
),
);
}
@override
void dispose() {
_channel.sink.close();
super.dispose();
}
}
Real-time Notifications
Example: Push notifications for events like new orders, messages, or alerts.
Implementation Steps:
- Connect to the WebSocket server.
- Listen for notification messages.
- Display notifications to the user using Flutter's notification system . Live Streaming Updates (e.g., Stock Prices, Sports Scores) Example: Displaying live stock prices or sports scores that update in real-time.
Implementation Steps:
- Connect to a WebSocket server providing real-time data.
- Update the UI dynamically as new data arrives .
Collaborative Applications (e.g., Real-time Document Editing)
Example: Allowing multiple users to edit a document simultaneously and see each other's changes in real-time.
Implementation Steps:
- Connect to a WebSocket server.
- Send changes made by a user to the server.
- Broadcast changes from the server to all connected clients
Online Gaming
Example: Multiplayer games where real-time interaction is crucial.
Implementation Steps:
- Connect players to a WebSocket server.
- Use WebSocket messages to communicate player actions and game state changes .
Real-time Dashboards and Analytics
Example: Dashboards that display live analytics, like website traffic, system health, etc.
Implementation Steps:
- Connect to a WebSocket server that streams real-time analytics data.
- Update the dashboard UI as new data arrives
IoT Device Control and Monitoring
Example: Controlling IoT devices and monitoring their status in real-time.
Implementation Steps:
- Connect to a WebSocket server.
- Send control commands to devices.
- Receive real-time status updates from devices . Example Code for Real-time Notifications
import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: NotificationScreen(),
);
}
}
class NotificationScreen extends StatefulWidget {
@override
_NotificationScreenState createState() => _NotificationScreenState();
}
class _NotificationScreenState extends State<NotificationScreen> {
final _channel = WebSocketChannel.connect(
Uri.parse('ws://echo.websocket.org'),
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Real-time Notifications'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: StreamBuilder(
stream: _channel.stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
// Display notification
return ListTile(
title: Text('New Notification'),
subtitle: Text(snapshot.data.toString()),
);
} else {
return Center(
child: Text('No notifications yet'),
);
}
},
),
),
);
}
@override
void dispose() {
_channel.sink.close();
super.dispose();
}
}
Top comments (0)