react native: how to automatically run websocket server when app starts - javascript

i've integrated socket.io into my react native app and it works successfully. However i always have to start the server manually using nodemon. After running react-native run-android i also have to enter nodemon index.js from the terminal which contains my socket.io configuration in order to get the server working. I need this server to start automatically whenever the app starts since i plan to build the app and run it on a real device where i cannot execute the nodemon command in a terminal to start the websocket server

Related

How to run some server-side JS then start a react app

I'm wondering how to run some server side JS which then starts a react app. Or if there is a way to explicitly segment sever vs client side JS, or do I have to just add and use Express to the react app?
Essentially I want to
node somefile.js
//do some server js stuff, then
npm run start
For example, if I wanted to check a users authentication status from a separate Next app before starting and serving a Docusaurus app.

how can I connect to a http://localhost:4200 using browser which is a aws ec2

I am learning node.js in an online course to build up a web server and app
before the teaching started, the teacher told me to set up your coding environment, and it got a direction to told me what to do step by step.
Because I using Chromebook, So I started an aws ec2 serve which is ubuntu 18.04 and using cloud9 for my ide
I installed node.js successful on the ec2
And the direction told me to type on those on the terminal
git clone https://github.com/OpenClassrooms-Student-Center/5614116-front-end-app.git frontend
You can then do the following:
cd frontend
npm install
ng serve
This will install all the dependencies needed by the front end app and will launch the development server. Now, if you navigate to http://localhost:4200 , you should see the following (assuming you've followed the steps above successfully):
the direction say if I successfully followed the step, will see something on http://localhost:4200
On the terminal, at last show me
ℹ 「wdm」: Compiled successfully.
But how can I connect to http://localhost:4200 which was an ec2?
I tried using IP:4200, the browser keeps on loading the page and nothing shows up on the browser
----------------------------------------
I found out that the program that teacher gave me to install set the URL to http://localhost:4200
because cloud9 says the program was running, but running at a URL call http://localhost:4200
what should I do?
Localhost refers to your local machine. But as mentionedd that your node.js running on ec2 and want to connect to it from your browser, get the IP of the ec2 and type in your browser- IP:4200
And it should work
The only problem here is you have to add port 4200 in your aws ec2 instance security group to allow inbound traffic.
STEPS:
1. Go to your EC2 instance.
2. Click security group from Description below.
3. Click Inbound then Edit.
4. Click Add Rule
5. Select Custom TCP , Port - 4200 , IP - 0.0.0.0/0
6. Then SAVE.
After that try <EC2-IP:Port> in your browser it will work fine.

Meteor server crashes very frequently without any error

I am working with the Meteor application and I deployed the same over EC2 instance. It was working fine till a few days back.
But now the server process kill automatically without any error log or
console
I tried to get the error but unfortunately, as there are not any logs I am unable to find out why the server is crashing again and again.
I have a medium EC2 machine on which the application is running.
I am using nohup for running the application in the background.
Below is the command I used to start the server:-
nohup meteor --settings SETTINGS-PRODUCTION.JSON &
I am wondering to know about server crashes due to nohup or some other reason is there.
Please let me know how we can console uncaught exception in the meteor-like we do in express.
What should I use to auto restart the server if the process is killed
by any error or exception?
Any help would be much appreciated!
Thanks
Using nohup is quite a low-tech solution. Things like Phusion Passenger, PM2, or forever do a better job.
Also your docker container can be configured to automatically restart the process.
Even better is a tool called Meteor Up, which makes it really simple to deploy Meteor apps to EC2.
EASILY DEPLOY YOUR APP Meteor Up is a production quality Meteor app
deployment tool.
Install with one command:
$ npm install --global mup
http://meteor-up.com/

JS server already running react native app

I'm following the Getting Started tutorial from here: https://facebook.github.io/react-native/docs/getting-started.html. I'm able to boot up my package manager with react-native start. But running the application itself always results in a crash.
$ react-native run-android
JS server already running.
Building and installing the app on the device (cd android && gradlew.bat installDebug...
Error occurred during initialization of VM
Unable to use shared archive.
An error has occurred while processing the shared archive file.
Unable to unmap shared space.
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/android-setup.html
What is the problem?
You already have a react native process running in the background on the same port.
Use ps -ef | grep react
and then kill the process using the process id
kill -9 pid
and then re execute the command:
react-native run-android
First of all give proper permission to your gradlew, in your root directory run following command
chmod 755 android/gradlew
After this make sure you have local.properties file in your android folder with sdk file path like
sdk.dir = /home/username/Android/Sdk/

Debuging expressjs server alongside electron

I've successfully running expressjs server alongside electron as described on here:
Run Node.js server file automatically after launching Electron App
The problem now, there is no output from command line related to server activity. I simply run
electron .
on project directory, then no other output related to the server.
Is there any way to get that server activity logged onto cli as normally I run with (like just) node server.js ??
Perhaps you can try using the node module concurrently. This will allow you to run two commands at once and is used commonly in development with electron.
Instead of getting the server and electron to run from within the same file, separate it into a server file and an electron file. For example, one of my main development techniques is this:
concurrently "npm run server" "npm run start-app"
Which runs my hot-reload server, that my electron app connects to in development mode.
By using concurrently you can see the output of each process as well.
A good example of this technique in practice is with the electron-react-boilerplate repository. If you're interesting in this style of development I recommend you clone that repository and give npm run dev a try, to see how their development process works (whether you're using React or not).

Categories