diff --git a/lib/CF/Controller/Websockets.pm b/lib/CF/Controller/Websockets.pm
index 119cefbf2b0ef5e58501033fcd9b432731a6c54f..ab3b44e68f55a299e50a2a204df84a586af84718 100644
--- a/lib/CF/Controller/Websockets.pm
+++ b/lib/CF/Controller/Websockets.pm
@@ -36,7 +36,7 @@ sub main {
                 ip        => $ip,
                 keepalive => \'now()',
                 user_id   => $user_id,
-            }, { key => 'primary'} );
+            }, { key => $user_id ? 'primary':'user'} );
 
             my $all = $c->schema->resultset('Socket_view')->count(
                 { is_alive => 't', }
diff --git a/lib/CF/Schema/Result/Socket.pm b/lib/CF/Schema/Result/Socket.pm
index 84f86f147ca3c57f63e5c7438e73fea0456649da..943bc770e7f030035700e0cf3c9e3667033c5cc5 100644
--- a/lib/CF/Schema/Result/Socket.pm
+++ b/lib/CF/Schema/Result/Socket.pm
@@ -20,4 +20,8 @@ __PACKAGE__->add_columns(
 
 __PACKAGE__->set_primary_key('id');
 
+__PACKAGE__->add_unique_constraint(
+    'user' => [qw(user_id)]
+);
+
 1;