@@ -3,43 +3,60 @@ import Cookies from 'universal-cookie';
33
44const cookies = new Cookies ( ) ;
55
6- let socket : Socket ;
7- const SOCKET_URL = process . env . REACT_APP_PROTOCOL + '://' + process . env . REACT_APP_DOMAIN ;
6+ let socket : Socket ;
7+ const SOCKET_URL =
8+ process . env . REACT_APP_PROTOCOL + '://' + process . env . REACT_APP_DOMAIN ;
89const SOCKET_PATH = process . env . REACT_APP_SOCKET_PATH ;
910
1011export const initiateSocket = ( ) => {
1112 if ( socket ) {
1213 return ;
1314 }
1415
15- socket = io ( SOCKET_URL , { path : SOCKET_PATH } ) ;
16+ socket = io ( SOCKET_URL , {
17+ path : SOCKET_PATH ,
18+ transports : [ 'polling' , 'websocket' ] ,
19+ closeOnBeforeunload : true ,
20+ forceNew : true ,
21+ tryAllTransports : true ,
22+ withCredentials : true ,
23+ } ) ;
1624
1725 const userCookies = cookies . get ( 'user' ) ;
1826 if ( userCookies != null ) {
1927 socket . emit ( 'authenticate' , userCookies . access_token ) ;
2028 }
2129
30+ socket . on ( 'connect' , ( ) => {
31+ const transport = socket . io . engine . transport . name ; // in most cases, "polling"
32+
33+ console . log ( 'Connected with transport:' , transport ) ;
34+
35+ socket . io . engine . on ( 'upgrade' , ( ) => {
36+ const upgradedTransport = socket . io . engine . transport . name ; // in most cases, "websocket"
37+ console . log (
38+ 'Transport upgraded from' ,
39+ transport ,
40+ 'to' ,
41+ upgradedTransport ,
42+ ) ;
43+ } ) ;
44+ } ) ;
45+
2246 socket . on ( 'disconnect' , ( reason , details ) => {
2347 console . log ( 'Client disconnected, reconnecting' , reason , details ) ;
24-
25- setTimeout ( ( ) => {
26- socket . connect ( ) ;
27- } , 1000 ) ;
48+ ensureSocketConnected ( ) ;
2849 } ) ;
2950
3051 socket . on ( 'connect_error' , ( error ) => {
3152 console . log ( 'Connection error, reconnecting' , error . message ) ;
32- setTimeout ( ( ) => {
33- socket . connect ( ) ;
34- } , 1000 ) ;
53+ ensureSocketConnected ( ) ;
3554 } ) ;
36-
37- // dumbass brute force so nginx doesn't kill us
38- setInterval ( ( ) => {
39- socket . emit ( 'ping' ) ;
40- } , 5000 ) ;
4155} ;
4256
57+ // use subscriptions when reconnecting to listen for all previous events
58+ const subscriptions = { } ;
59+
4360export const socketSubscribeTo = ( emission , callback ) => {
4461 if ( ! socket ) {
4562 initiateSocket ( ) ;
@@ -48,6 +65,8 @@ export const socketSubscribeTo = (emission, callback) => {
4865 socket . on ( emission , ( data ) => {
4966 callback ( data ) ;
5067 } ) ;
68+
69+ subscriptions [ emission ] = callback ;
5170} ;
5271
5372export const socketUnsubscribeFrom = ( emission ) => {
@@ -56,6 +75,8 @@ export const socketUnsubscribeFrom = (emission) => {
5675 }
5776
5877 socket . off ( emission ) ;
78+
79+ delete subscriptions [ emission ] ;
5980} ;
6081
6182export const ensureSocketConnected = ( ) => {
@@ -66,6 +87,13 @@ export const ensureSocketConnected = () => {
6687
6788 if ( ! socket . connected || ! socket . active ) {
6889 console . log ( 'Existing socket not connected or inactive, reconnecting' ) ;
90+ socket . close ( ) ;
91+ socket . removeAllListeners ( ) ;
6992 socket . connect ( ) ;
93+
94+ // re-subscribe to all previous events (don't call socketSubscribeTo to avoid infinite loop)
95+ Object . keys ( subscriptions ) . forEach ( ( emission ) => {
96+ socket . on ( emission , subscriptions [ emission ] ) ;
97+ } ) ;
7098 }
7199} ;
0 commit comments