- Updated the WebSocket hub to replace the insecure `user_id` query parameter with an authentication token for secure connections. - Introduced typed events for mail operations (created, updated, deleted) to streamline event handling. - Implemented heartbeat functionality (ping/pong) to maintain connection health. - Enhanced client reconnection logic and delta replay for improved user experience. - Added limits on connections per user/session to prevent abuse and ensure stability.
80 lines
1.7 KiB
Go
80 lines
1.7 KiB
Go
package realtime
|
|
|
|
// WebSocket event type names.
|
|
const (
|
|
TypeMailCreated = "mail.created"
|
|
TypeMailUpdated = "mail.updated"
|
|
TypeMailDeleted = "mail.deleted"
|
|
TypeOutboxUpdated = "outbox.updated"
|
|
TypeContactUpdated = "contact.updated"
|
|
|
|
TypeWSPing = "ws.ping"
|
|
TypeWSPong = "ws.pong"
|
|
)
|
|
|
|
// MailEventPayload is the payload for mail.created, mail.updated, and mail.deleted.
|
|
type MailEventPayload struct {
|
|
MessageID string `json:"message_id"`
|
|
AccountID string `json:"account_id"`
|
|
}
|
|
|
|
// OutboxEventPayload is the payload for outbox.updated.
|
|
type OutboxEventPayload struct {
|
|
AccountID string `json:"account_id"`
|
|
}
|
|
|
|
// ContactEventPayload is the payload for contact.updated.
|
|
type ContactEventPayload struct {
|
|
ContactID string `json:"contact_id"`
|
|
AccountID string `json:"account_id,omitempty"`
|
|
}
|
|
|
|
func NewMailCreatedEvent(messageID, accountID string) Event {
|
|
return Event{
|
|
Type: TypeMailCreated,
|
|
Payload: MailEventPayload{
|
|
MessageID: messageID,
|
|
AccountID: accountID,
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewMailUpdatedEvent(messageID, accountID string) Event {
|
|
return Event{
|
|
Type: TypeMailUpdated,
|
|
Payload: MailEventPayload{
|
|
MessageID: messageID,
|
|
AccountID: accountID,
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewMailDeletedEvent(messageID, accountID string) Event {
|
|
return Event{
|
|
Type: TypeMailDeleted,
|
|
Payload: MailEventPayload{
|
|
MessageID: messageID,
|
|
AccountID: accountID,
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewOutboxUpdatedEvent(accountID string) Event {
|
|
return Event{
|
|
Type: TypeOutboxUpdated,
|
|
Payload: OutboxEventPayload{
|
|
AccountID: accountID,
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewContactUpdatedEvent(contactID, accountID string) Event {
|
|
return Event{
|
|
Type: TypeContactUpdated,
|
|
Payload: ContactEventPayload{
|
|
ContactID: contactID,
|
|
AccountID: accountID,
|
|
},
|
|
}
|
|
}
|