0
# Real-time Messaging
1
2
Connect to Slack's Real Time Messaging API for live message streams and event handling.
3
4
## Capabilities
5
6
### RTM Connection
7
8
Establish real-time connections to receive live events from Slack.
9
10
```python { .api }
11
def start(self, simple_latest=False, no_unreads=False, mpim_aware=False):
12
"""
13
Start RTM connection with comprehensive workspace data.
14
15
Args:
16
simple_latest (bool): Return simple message format (default: False)
17
no_unreads (bool): Skip unread counts in initial data (default: False)
18
mpim_aware (bool): Include multi-party IM awareness (default: False)
19
20
Returns:
21
Response: WebSocket URL and complete workspace state including:
22
- WebSocket URL for connection
23
- User and team information
24
- Channel and group lists
25
- IM and MPIM conversations
26
- Bot information
27
"""
28
29
def connect(self):
30
"""
31
Connect to RTM with minimal initial data.
32
33
Returns:
34
Response: WebSocket URL and basic connection information
35
"""
36
```
37
38
Usage:
39
```python
40
# Full RTM start with complete workspace data
41
response = slack.rtm.start()
42
if response.successful:
43
ws_url = response.body['url']
44
users = response.body['users']
45
channels = response.body['channels']
46
# Connect to WebSocket URL to receive events
47
48
# Lightweight RTM connection
49
response = slack.rtm.connect()
50
ws_url = response.body['url']
51
# Connect to WebSocket for events only
52
```
53
54
## Event Handling
55
56
Once connected to the WebSocket URL, you'll receive real-time events including:
57
58
- **Message events**: New messages in channels and DMs
59
- **User events**: User status changes, typing indicators
60
- **Channel events**: Channel joins, leaves, renames
61
- **File events**: File uploads and shares
62
- **Team events**: Team changes and member updates
63
64
## WebSocket Integration
65
66
The RTM API provides a WebSocket URL that you'll need to connect to using a WebSocket client library:
67
68
```python
69
import websocket
70
import json
71
72
def on_message(ws, message):
73
event = json.loads(message)
74
if event['type'] == 'message':
75
print(f"New message: {event['text']}")
76
77
def on_error(ws, error):
78
print(f"WebSocket error: {error}")
79
80
# Get WebSocket URL from RTM
81
response = slack.rtm.start()
82
ws_url = response.body['url']
83
84
# Connect to WebSocket
85
ws = websocket.WebSocketApp(ws_url,
86
on_message=on_message,
87
on_error=on_error)
88
ws.run_forever()
89
```
90
91
## Types
92
93
```python { .api }
94
class RTM(BaseAPI):
95
"""Real-time messaging connection management."""
96
def start(self, simple_latest=False, no_unreads=False, mpim_aware=False): ...
97
def connect(self): ...
98
```