Is possible to React Native run a socket server? - node.js

I'd like to make a app with React-Native that's accept connections from another devices (Desktops or mobiles) through raw tcp sockets (like node's Net API) or WebSockets (like Socket.io). The point is that, socket server must be running on the React-Native's App.
I already tried Socket.io and react-native-tcp, it works when i make the server run on a nodeJS's application and the client on RN's app, but not the reverse.
When i try to import Socket.io and make it listen on a port, a error is raisen, because RN don't have node's http module. Just Socket.io/clients works.
I think that i'm doing something wrong, but is really possible to do that? and what is the best way?
Obs: I'm really new in RN's world.

No, we can't create a server although if we create a server we can't connect any other external applications to the server.
So create a server and deploy it in any could service then use it in your react-native app.

Related

why we use socket.io client we can make app with only using via socket.io server?

I having some doubts that:-
what is need to use the socket.io client we can use only the socket.io server to stop refreshing the app.
what is different between the socket.io client and socket.io server.
check this link
socket-io.client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).
A server that is not initiating socket.io connections to other servers would not use this code. This has been made a little more confusing that it probably should be because when using socket.io, it appears that both client and server are using the same socket.io.js file (because they both refer to a file with the same name), but is not actually the case. The server is using a different file than the client.
From the Github page for socket-io.client:
A standalone build of socket.io-client is exposed automatically by the socket.io server as /socket.io/socket.io.js. Alternatively you can serve the file socket.io.js found at the root of this repository.
Keep in mind that there are unique features that belong to client and server so it should not be a surprise that they use some different code. Though they share code for parsing the protocol and things like that, the server has the ability to run a server or hook into an existing web server and it has methods like .join() and .leave() and data structures that keep track of all the connected sockets and is expected to live in the node.js environment. The client has the ability to initiate a connection (send the right http request), do polling if webSockets are not supported, build on a native webSocket implementation if present, etc....

client-server websocket

I try to make a client-server app with socket.io.
Socket.io server seems work but the client cannot connect to it. The error i get is about the '/socket.io/socket.io.js' like what it is unable to load it.
So here are my questions
is it mandatory to have server and client in the same folder as we can see in the official demo ?
can we make a nodejs socket.io server without express ?
Depending on how your project is setup, you need to create 2 server files, 1 for the app, and one for the websockets, and every time a user opens the app it should open(and be told where to try and open the connection) a connection to the websockets server. On my websockets apps I have the app running on localhost:3000, and websockets server on localhost:3001 (and tell the app to look for a server on 3001), so really you don't need to have the server files in the same folder, they can be in 2 opposite ends of your computer, as long as the app points to the server, then your fine, once a connection has been opened, the websocket server will see the client, and it should work! Let me know if that make sense.
No, you can download socket.io front-end lib from another sources, for example cdn. Be sure you installed the right version.
Yes, you can make it without express. Express is just another option for creating an socket.io server.
For example, currently in my project that is written in another back-end node framework i'm using the code below to establish the socket.io server.
const io = require("socket.io")(2337);
io.on("connection", socket =>
// some code
)
I fixed the problem of websocket communication between my server and my client.
After inspected my html client file, i saw on the console this error message 'ReferenceError: io is not defined'.
I google that error and i found this.

Socket.io Not Connecting

I'm wondering how I can connect to a socket.io websocket server externally. I've been using wscat to test connections but to no avail. The code I'm using is here. Anyways, I've tried wscat'ing to where I deployed it, which is c9, but that hasn't worked. I've recieved an ECONNREFUSED error from wscat. Is there a specific thing I need to do to enable socket.io connections from outside of a page hosted on the same server? The end game is to be able to connect to the socket.io server with an iOS app.
Thanks
You must use a Socket.IO library for your language. Socket.IO is not plain websockets and cannot be used as such.
Instead, look at something like: https://github.com/pkyeck/socket.IO-objc

What is the difference between socket.io client and socket.io server? Alternatives to socket.io?

I have been breaking my head on understanding what exactly is socket.io and its role in the whole persistent communication between client and server in general. Some fundamental questions that keep coming up in my mind are :
Are node.js and socket.io server different ways of doing the same thing ? Like lighthttpd and apache ?
Why should I use socket.io server if I have node.js ?
Can I use socket.io client with another server side programming language like PHP ?
Are there alternatives to using socket.io client ?
Is socket.io client just another javascript library for websocket communication ?
Thanks :)
The Socket.IO server accepts connections from Socket.IO clients. This is not any different than any other server (such as a web server like Apache) accepting a connection from a client (such as a web browser like Internet Explorer).
Node.js is a platform built on top of the V8 JavaScript engine, which comes with a convenient library usually used for web and network applications. Socket.IO is a web-socket-like communication wrapper and RPC which enables servers and clients to communicate over a variety of transports (every thing from long-polling JSON to real Web Sockets). Generally, the Socket.IO server code runs in your JavaScript application running on top of Node.js, and the Socket.IO client runs in your JavaScript on a web browser. (Note that you can run the Socket.IO client in your Node.js application to, which I have used in the past as a quick RPC between multiple Node.js applications.)
There are Socket.IO clients available for many languages, and yes I believe there is one for PHP but I haven't used it personally.
If you want to communicate with a Socket.IO server, you must use a Socket.IO client. It is a protocol all on its own.
Are node.js and socket.io server different ways of doing the same thing ?
SocketIO is actually a I/O engine that permits realtime communication between client and server, unlike classic HTTP requests.
Why should I use socket.io server if I have node.js ?
Since both are differents, we can't compare.
Can I use socket.io client with another server side programming language like PHP ?
No, because socketIO server run in NodeJS environment.
Client first make a HTTP request, then socketIO send a static JS script to client.
This script establish the two-way communication.
But you can use socketIO server with different socketIO client implementation.
Are there alternatives to using socket.io client ?
Any socketIO client implementation can do the job since they connected to server.
But beware of asynchronous way of communication, like in Java or PHP.
Is socket.io client just another javascript library for websocket communication ?
Yes it is, but socketIO aims on reliability and easy to use, and also do best effort : It choose the best transport available for client.
Hope it helps !
Are node.js and socket.io server different ways of doing the same thing ? Like lighthttpd and apache ?
Socket.io is a framework/library for node.js
Why should I use socket.io server if I have node.js ?
It's a framework designed for two-way communication, use it if you want
Can I use socket.io client with another server side programming language like PHP ?
Technically yes. But then you would have to reimplement socket.io server in that other language to match it.
Are there alternatives to using socket.io client ?
Lots of, google for websocket/real time communication in [here put your language]
Is socket.io client just another javascript library for websocket communication ?
Socket.io client is a JavaScript library compatibile with socket.io server (which is JavaScript library as well, only on the server side, i.e. Node.js)

Create websocket connection between another server and a client

I'm building a desktop application where clients will interface with the server through the browser, but will also connect to eachother using a local node.js server. This means that every client has a local node.js server running.
I want to allow users to connect to the central webserver with the browser, and then initialize direct connections to other client's local node.js server. Is there a production ready way to expose a local node.js server to the outside world, or otherwise proxy the connection between the two?
Right now, my solution is to use localtunnel to expose the server, and then make a connection. As far as I understand, this is not recommended, and I'm looking for something better.

Resources