Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: start websocket connection on react init #2481

Merged
merged 5 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/workflows/e2e-ios-self.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Detox E2E iOS (self-hosted)

on:
# push:
# paths:
# - packages/mobile/**
# - packages/backend/**
# - packages/state-manager/**
# - .github/workflows/e2e-ios-self.yml

jobs:
detox-ios-self-hosted:
timeout-minutes: 25
runs-on: [self-hosted, macOS, ARM64, iOS]

steps:
- uses: actions/checkout@v4
with:
lfs: true

- name: Install dependencies
run: |
npm i
npm run lerna bootstrap --scope @quiet/eslint-config,@quiet/logger,@quiet/common,@quiet/types,@quiet/state-manager,@quiet/backend,@quiet/identity,@quiet/mobile,backend-bundle

- name: Install pods
run: |
cd packages/mobile/ios
pod install

- name: Install pm2
run: npm install pm2@latest -g

- name: Start metro
run: |
cd packages/mobile
pm2 --name METRO start npm -- start

- name: Build Detox
run: |
cd packages/mobile
detox build -c ios.sim.debug.ci

- name: Run basic tests
run: |
cd packages/mobile
detox test starter -c ios.sim.debug.ci

- name: Stop metro
if: always()
run: pm2 stop METRO
1 change: 1 addition & 0 deletions packages/mobile/ios/CommunicationBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
#import "React/RCTEventEmitter.h"

@interface RCT_EXTERN_MODULE(CommunicationModule, RCTEventEmitter)
RCT_EXTERN_METHOD(handleIncomingEvents:(NSString *)event payload:(NSString *)payload extra:(NSString *)extra)
@end
7 changes: 7 additions & 0 deletions packages/mobile/ios/CommunicationModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class CommunicationModule: RCTEventEmitter {
self.sendEvent(withName: CommunicationModule.APP_RESUME_IDENTIFIER, body: nil)
}

@objc
func handleIncomingEvents(_ event: NSString, payload: NSString?, extra: NSString?) {
let socketPort = WebsocketSingleton.sharedInstance.socketPort
let socketIOSecret = WebsocketSingleton.sharedInstance.socketIOSecret
self.sendDataPort(port: socketPort, socketIOSecret: socketIOSecret);
}

override func supportedEvents() -> [String]! {
return [CommunicationModule.BACKEND_EVENT_IDENTIFIER, CommunicationModule.NOTIFICATION_EVENT_IDENTIFIER, CommunicationModule.STOP_EVENT_IDENTIFIER, CommunicationModule.APP_PAUSE_IDENTIFIER, CommunicationModule.APP_RESUME_IDENTIFIER]
}
Expand Down
4 changes: 4 additions & 0 deletions packages/mobile/ios/Quiet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
7F80A59D9EC1440186E5D5CF /* Rubik-SemiBold.ttf in Resources */ = {isa = PBXBuildFile; fileRef = BF73B04135634980BC656D6C /* Rubik-SemiBold.ttf */; };
80CCB457674F4979A3C5DB06 /* Rubik-MediumItalic.ttf in Resources */ = {isa = PBXBuildFile; fileRef = EBC2A7699E0A49059904DD8C /* Rubik-MediumItalic.ttf */; };
8A009A60D84E4B08AB0E8152 /* Rubik-Bold.ttf in Resources */ = {isa = PBXBuildFile; fileRef = E820B3E5514B49EE8C72DECB /* Rubik-Bold.ttf */; };
955DC7582BD930B30014725B /* WebsocketSingleton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 955DC7572BD930B30014725B /* WebsocketSingleton.swift */; };
9EC9E7C54868433990A479EC /* Rubik-ExtraBoldItalic.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 182B8961416E4D4C8A29793D /* Rubik-ExtraBoldItalic.ttf */; };
9EFEA2C3C6AE0D6FA6A4C079 /* libPods-Quiet-QuietTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = ED4D442ACC913B8E48E2C569 /* libPods-Quiet-QuietTests.a */; };
A43CD77BAC37717C692E6333 /* libPods-Quiet.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 47D3FEF6693D5638AD9D0AFF /* libPods-Quiet.a */; };
Expand Down Expand Up @@ -606,6 +607,7 @@
599527D61D41D1899FC6F87B /* Pods-Quiet-QuietTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Quiet-QuietTests.release.xcconfig"; path = "Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests.release.xcconfig"; sourceTree = "<group>"; };
706E1601C7B649A8A40A7877 /* Rubik-Light.ttf */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = unknown; name = "Rubik-Light.ttf"; path = "../assets/fonts/Rubik-Light.ttf"; sourceTree = "<group>"; };
84720E58493F44BE8C4784E3 /* Rubik-BoldItalic.ttf */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = unknown; name = "Rubik-BoldItalic.ttf"; path = "../assets/fonts/Rubik-BoldItalic.ttf"; sourceTree = "<group>"; };
955DC7572BD930B30014725B /* WebsocketSingleton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebsocketSingleton.swift; sourceTree = "<group>"; };
BF73B04135634980BC656D6C /* Rubik-SemiBold.ttf */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = unknown; name = "Rubik-SemiBold.ttf"; path = "../assets/fonts/Rubik-SemiBold.ttf"; sourceTree = "<group>"; };
CD39A872C9F80A6BA04B8A4A /* Pods-Quiet.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Quiet.release.xcconfig"; path = "Target Support Files/Pods-Quiet/Pods-Quiet.release.xcconfig"; sourceTree = "<group>"; };
E820B3E5514B49EE8C72DECB /* Rubik-Bold.ttf */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = unknown; name = "Rubik-Bold.ttf"; path = "../assets/fonts/Rubik-Bold.ttf"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -679,6 +681,7 @@
1889CA4D26E763E1004ECFBD /* Extensions.swift */,
1868C43B2930E255001D6D5E /* CommunicationModule.swift */,
1868C43D2930EAEA001D6D5E /* CommunicationBridge.m */,
955DC7572BD930B30014725B /* WebsocketSingleton.swift */,
);
name = Quiet;
sourceTree = "<group>";
Expand Down Expand Up @@ -5140,6 +5143,7 @@
files = (
18FD2A40296F009E00A2B8C0 /* main.m in Sources */,
1868BCF1292E9212001D6D5E /* rn-bridge.cpp in Sources */,
955DC7582BD930B30014725B /* WebsocketSingleton.swift in Sources */,
1889CA4E26E763E1004ECFBD /* Extensions.swift in Sources */,
1898143A2934CF70001F39E7 /* TorHandler.swift in Sources */,
1868BCEE292E9212001D6D5E /* RNNodeJsMobile.m in Sources */,
Expand Down
40 changes: 10 additions & 30 deletions packages/mobile/ios/Quiet/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,43 +45,30 @@ - (void) createDataDirectory {
self.dataPath = [dataDirectory create];
}

- (void) initWebsocketConnection {
/*
* We have to wait for RCTBridge listeners to be initialized, yet we must be sure to deliver the event containing data port information.
* Delay used below can't cause any race condition as websocket won't connect until data server starts listening anyway.
*/
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSArray *intervals = @[@5, @15, @30, @60, @90];
for (NSNumber *interval in intervals) {
NSTimeInterval delayInSeconds = [interval doubleValue];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
[[self.bridge moduleForName:@"CommunicationModule"] sendDataPortWithPort:self.dataPort socketIOSecret:self.socketIOSecret];
});
}
});
}

- (void) spinupBackend:(BOOL)init {

// (1/6) Find ports to use in tor and backend configuration
// (1/4) Find ports to use in tor and backend configuration

Utils *utils = [Utils new];

if (self.socketIOSecret == nil) {
self.socketIOSecret = [utils generateSecretWithLength:(20)];
self.socketIOSecret = [utils generateSecretWithLength:(20)];
}

FindFreePort *findFreePort = [FindFreePort new];

self.dataPort = [findFreePort getFirstStartingFromPort:11000];

WebsocketSingleton *websocket = [WebsocketSingleton sharedInstance];
websocket.socketPort = self.dataPort;
websocket.socketIOSecret = self.socketIOSecret;

uint16_t socksPort = [findFreePort getFirstStartingFromPort:arc4random_uniform(65000 - 1024) + 1024];
uint16_t controlPort = [findFreePort getFirstStartingFromPort:arc4random_uniform(65000 - 1024) + 1024];
uint16_t httpTunnelPort = [findFreePort getFirstStartingFromPort:arc4random_uniform(65000 - 1024) + 1024];


// (2/6) Spawn tor with proper configuration
// (2/4) Spawn tor with proper configuration

self.tor = [TorHandler new];

Expand All @@ -94,7 +81,7 @@ - (void) spinupBackend:(BOOL)init {
});


// (4/6) Connect to tor control port natively (so we can use it to shutdown tor when app goes idle)
// (3/4) Connect to tor control port natively (so we can use it to shutdown tor when app goes idle)

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSData *authCookieData = [self getAuthCookieData];
Expand All @@ -113,14 +100,7 @@ - (void) spinupBackend:(BOOL)init {
}];
});


// (5/6) Update data port information and broadcast it to frontend
if (init) {
[self initWebsocketConnection];
}


// (6/6) Launch backend or rewire services
// (4/4) Launch backend or rewire services

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

Expand Down
9 changes: 9 additions & 0 deletions packages/mobile/ios/WebsocketSingleton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@objcMembers
class WebsocketSingleton: NSObject {
static let sharedInstance = WebsocketSingleton()

var socketPort: UInt16 = 0
var socketIOSecret: String = ""

private override init() {}
}
Loading