Nodejs+ReactNative Login,SocketMessage

โœ… ์ฝ”๋“œ ๋‹ค์šด๋กœ๋“œ / Code Download

https://github.com/gideonslife01/flm-js-nodejs

๐ŸŒ ์ด ์•ฑ์€ ๋กœ๊ทธ์ธ๊ณผ ์†Œ์ผ“๋ฉ”์„ธ์ง€ ๋ฐœ์†ก๊ธฐ๋Šฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค.(์›น,๋ชจ๋ฐ”์ผ ์•ฑ[๋ฆฌ์•กํŠธ๋„ค์ดํ‹ฐ๋ธŒ])
This app has login and socket message sending functions. (Web, mobile app [React Native])

๐ŸŒ SockeetMessage๋Š” ๋ฆฌ์•กํŠธ๋„ค์ดํ‹ฐ๋ธŒ์ž…๋‹ˆ๋‹ค. ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•˜์„ธ์š”
SocketMessage is for React Native. Please create a new project to use it.

๐ŸŒ1.๋””๋ ‰ํ† ๋ฆฌ/Directory

1)nodejs+socketMessage/ReactNativeApp/ : ๋ฆฌ์•กํŠธ๋„ค์ดํ‹ฐ๋ธŒ / ReactNative

— ํŒŒ์ผ ๋ฐ ๋””๋ ‰ํ† ๋ฆฌ

android		index.js	ios		package.json	src

— src ๋””๋ ‰ํ† ๋ฆฌ

src : ์•ฑ ์†Œ์Šค์ฝ”๋“œ/App souce code
- files : MainScreen.js,LoginScreen.js,index.js

- MainScreen.js,LoginScreen.js,index.js๋Š” ๊ธฐ์กด ๋กœ๊ทธ์ธ ํŒŒ์ผ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
MainScreen.js, LoginScreen.js, and index.js are the same as the existing login files.

- SocketMessage.js๋Š” ์†Œ์ผ“๋ฉ”์„ธ์ง€ ์ˆ˜์‹  ๋ฐ ๋ฐœ์†ก ํด๋ผ์ด์–ธํŠธ์ž…๋‹ˆ๋‹ค.
SocketMessage.js is a socket message receiving and sending client.

— ios ๋””๋ ‰ํ† ๋ฆฌ / ios direactory

ios : ios์„ค์ •ํŒŒ์ผ๋กœ httpํ—ˆ์šฉ์„ค์ •ํŒŒ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. / There is an iOS configuration file for allowing HTTP connections.
- info.plist

— android ๋””๋ ‰ํ† ๋ฆฌ / android directory

android : Android SDk์œ„์น˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. / There is an Android SDK location.
- local.properties

2)nodejs+socketMessage/login+socketmessage : Nodejs

— ํŒŒ์ผ ๋ฐ ๋””๋ ‰ํ† ๋ฆฌ / files and directory

node_modules		package.json		server.js
package-lock.json	public

— public : ejs ํŒŒ์ผ / ejs files

login.ejs		SocketMessage.ejs

— server.js : ์„œ๋ฒ„ ์‹คํ–‰ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. / This is the server executable file.


๐ŸŒ2.๋ฆฌ์•กํŠธ๋„ค์ดํ‹ฐ๋ธŒ์„ค์น˜ / ReactNative Install
1)ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ : Project creation
https://reactnative.dev/docs/getting-started-without-a-framework
– ๋‹ค์Œ ๋ช…๋ น์–ด๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. / Create a project with the following command.

npx @react-native-community/cli@latest init Your Project name

— ๋ชจ๋“ˆ์„ค์น˜ / modules install

โœ”๏ธpackage.jsonํŒŒ์ผ์—์„œ ์•„๋ž˜ ๋ถ€๋ถ„์˜ ๋ชจ๋“ˆ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
You can see the modules in the following section in the package.json file.

โœ”๏ธnpm install @react-native-async-storage/async-storage ์ด๋ ‡๊ฒŒ ์„ค์น˜ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
You can install it like this: npm install @react-native-async-storage/async-storage.

  "dependencies": {
    "@react-native-async-storage/async-storage": "^2.1.2",
    "@react-navigation/native": "^7.1.8",
    "@react-navigation/stack": "^7.3.1",
    "react": "19.0.0",
    "react-native": "0.79.2",
    "react-native-gesture-handler": "^2.25.0",
    "react-native-safe-area-context": "^5.4.0",
    "react-native-screens": "^4.10.0",
    "socket.io-client": "^4.8.1"
  },
npm install @react-native-async-storage/async-storage@latest
npm install @react-navigation/native@latest
npm install @react-navigation/stack@latest
npm install react-native-gesture-handler@latest
npm install react-native-safe-area-context@latest
npm install react-native-screens@latest
npm install socket.io-client@latest

2)์•ˆ๋“œ๋กœ์ด๋“œ์„ค์ • / android settings

–๐Ÿ““ ์ด ๋ถ€๋ถ„์€ ์˜ค๋ฅ˜ ๋ฐœ์ƒ์‹œ์—๋งŒ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.
This part applies only when an error occurs.

— sdk location์„ค์ • / sdk location settings
project/android/locals.propertiesํŒŒ์ผ์ด ์—†์œผ๋ฉด ์ƒ์„ฑ

— locals.propertiesํŒŒ์ผ์— ์•ˆ๋“œ๋กœ์ด๋“œ sdk์œ„์น˜๋ฅผ ์ž…๋ ฅ / Enter the Android SDK location in the locals.properties file
โ€ปsdk์œ„์น˜๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค ๋ฉ”๋‰ด์˜ settings->Language & Frameworks->Android SDK->Android SDK Location์—์„œ ํ™•์ธ ๊ฐ€๋Šฅ
You can check the SDK location in the Android Studio menu: settings->Language & Frameworks->Android SDK->Android SDK Location

sdk.dir=/Users/gimdaegyeong/Library/Android/sdk

3)IOS ์„ค์ • / IOS Settings

— ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•˜๋ฉด ๋ฐ˜๋“œ์‹œ ios๋””๋ ‰ํ† ๋ฆฌ์— ๋“ค์–ด๊ฐ€์„œ ‘pod install’๋ช…๋ น์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
After installing the module, be sure to navigate to the ios directory and run the pod install command.

— pod install ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์ง€ ์•Š์œผ๋ฉด ios์•ฑ์— ๋ชจ๋“ˆ์ด ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
If you do not run the pod install command, the module will not be applied to the iOS app.

โ€“ https๊ฐ€ ์•„๋‹Œ httpํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— httpํ”„๋กœํ† ์ฝœ ํ—ˆ์šฉ ์„ค์ •์„ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
Since we are using the http protocol instead of https, we need to allow the http protocol.

— info.plistํŒŒ์ผ์„ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

โ€“ ํŒŒ์ผ์œ„์น˜ / file location : project/ios/project name directory/info.plist

— ํด๋ผ์šฐ๋“œ/ํ˜ธ์ŠคํŒ… ์„œ๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ipaddress๋‚˜ ๋„๋ฉ”์ธ์„ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
If you are using a cloud/hosting server, you can use ipaddress or domain.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CFBundleDevelopmentRegion</key>
	<string>en</string>
	<key>CFBundleDisplayName</key>
	<string>SocketMessage</string>
	<key>CFBundleExecutable</key>
	<string>$(EXECUTABLE_NAME)</string>
	<key>CFBundleIdentifier</key>
	<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundleName</key>
	<string>$(PRODUCT_NAME)</string>
	<key>CFBundlePackageType</key>
	<string>APPL</string>
	<key>CFBundleShortVersionString</key>
	<string>$(MARKETING_VERSION)</string>
	<key>CFBundleSignature</key>
	<string>????</string>
	<key>CFBundleVersion</key>
	<string>$(CURRENT_PROJECT_VERSION)</string>
	<key>LSRequiresIPhoneOS</key>
	<true/>

<!--httpํ—ˆ์šฉ์„ ์œ„ํ•œ ๋ณด์•ˆ์„ค์ •-->
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <false/>
    <key>NSExceptionDomains</key>
    <dict>

        <key>192.168.0.10</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <false/>
        </dict>

    </dict>
</dict>
<!--// httpํ—ˆ์šฉ์„ ์œ„ํ•œ ๋ณด์•ˆ์„ค์ •-->


	<key>NSLocationWhenInUseUsageDescription</key>
	<string></string>
	<key>UILaunchStoryboardName</key>
	<string>LaunchScreen</string>
	<key>UIRequiredDeviceCapabilities</key>
	<array>
		<string>arm64</string>
	</array>
	<key>UISupportedInterfaceOrientations</key>
	<array>
		<string>UIInterfaceOrientationPortrait</string>
		<string>UIInterfaceOrientationLandscapeLeft</string>
		<string>UIInterfaceOrientationLandscapeRight</string>
	</array>
	<key>UIViewControllerBasedStatusBarAppearance</key>
	<false/>
</dict>
</plist>

4)์‹คํ–‰/Run
– npm install android ๋˜๋Š” npm install ios๋ช…๋ น์–ด๋กœ ์‹คํ–‰ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
You can run it with the command npm install android or npm install ios.

3.NODEJS์„ค์น˜ / NODEJS install
1)์†Œ์Šค์ฝ”๋“œ ๋‹ค์šด ๋ฐ›๊ธฐ
– ์œ„์˜ ๊ตฌ๊ธ€ ๋“œ๋ผ์ด๋ธŒ์—์„œ nodejs+socketMessage/login+socketmessage ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋‹ค์šด ๋ฐ›์Šต๋‹ˆ๋‹ค.

2)๋ชจ๋“ˆ์„ค์น˜ / module install
— ๋จผ์ € npm init๋ช…๋ น์–ด๋กœ package.jsonํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
First, create a package.json file using the npm init command.
-package.jsonํŒŒ์ผ์—์„œ ์•„๋ž˜ ๋ถ€๋ถ„์˜ ๋ชจ๋“ˆ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
You can see the modules in the following section in the package.json file.

  "dependencies": {
    "bcrypt": "^5.1.1",
    "express": "^5.1.0",
    "express-session": "^1.18.1",
    "http": "^0.0.1-security",
    "socket.io": "^4.8.1"
  }

– npm install ๋ช…๋ น์–ด๋กœ ๋ชจ๋“ˆ์„ ์„ค์น˜ ํ•ฉ๋‹ˆ๋‹ค. / Install the module using the npm install command.

npm install express express-session bcrypt http socket.io --save

3)์‹คํ–‰ / run
— node server.js๋กœ nodejs์„œ๋ฒ„๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
Start the Node.js server with node server.js.

4)๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:3000์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
If you run http://localhost:3000 in your browser, you will see a screen like the following.

โ€ป ๋กœ๊ทธ์ธ ID๋Š” testuser,testuser2์ด๋ฉฐ ํŒจ์Šค์›Œ๋“œ๋Š” ๋ชจ๋‘ 11111111์ž…๋‹ˆ๋‹ค.
The login ID is testuser,testuser2 and the password is 11111111.

Leave a Reply