GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.
Go botgogolanggolang-whatsappgolang-whatsapp-apirestrest-apiwhatsappwhatsapp-apiwhatsapp-api-gowhatsapp-multi-devicewhatsapp-web-multi-device
fix(whatsapp): disconnect only the replaced device on StreamReplaced (#895)
Ganesh Mde6f3c17e8ef
diff --git a/src/infrastructure/whatsapp/event_handler.go b/src/infrastructure/whatsapp/event_handler.goindex 76e93994..0125d90f 100644--- a/src/infrastructure/whatsapp/event_handler.go+++ b/src/infrastructure/whatsapp/event_handler.go@@ -3,7 +3,6 @@ package whatsappimport ("context""fmt"- "os""strings""time"@@ -50,7 +49,7 @@ func handler(ctx context.Context, instance *DeviceInstance, rawEvt any) {case *events.Connected, *events.PushNameSetting:handleConnectionEvents(ctx, client, instance)case *events.StreamReplaced:- handleStreamReplaced(ctx)+ handleStreamReplaced(instance)case *events.Message:handleMessage(ctx, evt, chatStorageRepo, client)case *events.UndecryptableMessage:@@ -303,8 +302,25 @@ func handleConnectionEvents(_ context.Context, client *whatsmeow.Client, instancsendConfiguredPresence(context.Background(), client)}-func handleStreamReplaced(_ context.Context) {- os.Exit(0)+// handleStreamReplaced handles WhatsApp replacing this device's connection with+// another one that uses the same session (e.g. a second process with a copy of+// the session storage). Only the affected device is taken offline; the process+// and the other devices keep running. whatsmeow does not reconnect after this+// event, so the device stays disconnected until it is reconnected explicitly.+func handleStreamReplaced(instance *DeviceInstance) {+ deviceID := instance.ID()+ logrus.Warnf("[STREAM_REPLACED] Connection for device %s was replaced by another client using the same session; disconnecting this device", deviceID)++ if client := instance.GetClient(); client != nil {+ client.Disconnect()+ }+ instance.SetState(domainDevice.DeviceStateDisconnected)++ websocket.Broadcast <- websocket.BroadcastMessage{+ Code: "DEVICE_STREAM_REPLACED",+ Message: "Device connection was replaced by another client using the same session",+ Result: map[string]string{"device_id": deviceID},+ }}func handleReceipt(ctx context.Context, evt *events.Receipt, deviceID string, client *whatsmeow.Client) {diff --git a/src/infrastructure/whatsapp/event_handler_test.go b/src/infrastructure/whatsapp/event_handler_test.goindex 845bd887..f6c5dbf3 100644--- a/src/infrastructure/whatsapp/event_handler_test.go+++ b/src/infrastructure/whatsapp/event_handler_test.go@@ -1,10 +1,13 @@package whatsappimport (+ "context""testing""github.com/aldinokemal/go-whatsapp-web-multidevice/config"+ domainDevice "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/device""go.mau.fi/whatsmeow/types"+ "go.mau.fi/whatsmeow/types/events")func TestResolvePresenceOnConnect(t *testing.T) {@@ -62,3 +65,29 @@ func TestResolvePresenceOnConnect(t *testing.T) {})}}++// A StreamReplaced event for one device used to call os.Exit(0) and take every+// device in the process down with it (#883). It must only disconnect that device.+func TestHandlerStreamReplacedOnlyDisconnectsThatDevice(t *testing.T) {+ ctx := context.Background()+ replaced := NewDeviceInstance("test-stream-replaced", nil, nil)+ replaced.SetState(domainDevice.DeviceStateLoggedIn)+ other := NewDeviceInstance("test-stream-other", nil, nil)+ other.SetState(domainDevice.DeviceStateLoggedIn)++ go handler(ctx, replaced, &events.StreamReplaced{})+ msg := recvBroadcast(t)+ if msg.Code != "DEVICE_STREAM_REPLACED" {+ t.Fatalf("broadcast code = %s, want DEVICE_STREAM_REPLACED", msg.Code)+ }+ if result, ok := msg.Result.(map[string]string); !ok || result["device_id"] != "test-stream-replaced" {+ t.Errorf("broadcast result = %#v, want device_id test-stream-replaced", msg.Result)+ }++ if got := replaced.State(); got != domainDevice.DeviceStateDisconnected {+ t.Errorf("replaced device state = %s, want %s", got, domainDevice.DeviceStateDisconnected)+ }+ if got := other.State(); got != domainDevice.DeviceStateLoggedIn {+ t.Errorf("other device state = %s, want %s", got, domainDevice.DeviceStateLoggedIn)+ }+}