Is it possible to set up a socket.io client running (server-side) on a node.js server?

Asked
Active3 hr before
Viewed126 times

6 Answers

possible
90%

However, it's also possible to say "npm install socket.io-client" and install it as its own first-class citizen library., About ,However, as it turns out the server-side socket.io implementation doesn't offer a "connect" method, only a listen method.,Why is this? Why can't I treat a node application server as a client to a socket.io server running elsewhere? Is there any way that I can achieve this functionality?

Then your usage looks like this:

var client = require("socket.io-client");
var socket = client.connect("http://myendpoint.com:3000/whatever");
socket.emit("test", "foo");
load more v
88%

We make the http server listen on port 3000.,If you run node index.js you should see the following:,The result should be like the following video:,Put the following in your index.html file:

{
   "name": "socket-chat-example",
   "version": "0.0.1",
   "description": "my first socket.io app",
   "dependencies": {}
}
load more v
72%

Here is the project structure that we are going to work on during this series.,As you can see we also need the client library so (the socket.io library is for creating the server side) and (socket.io-client is for creating the client side) and express is for creating the server since we need to provide an HTTP Server to the socket.io to listen to and a specific host and port.,Now For creating the socket.io application, we need two node.js running instances, one for the client (app.js) and the other instance for the server (server.js) there for let’s install the socket io and express libraries.,Socket.io is a Library for Connecting a Client(s) to a Server using the Client/Server Architecture while it is a wrapper around WebSockets For Node.js, it is super easy and simple to use especially when dealing with chat messages or Real-time data.

Here is the project structure that we are going to work on during this series.

--dist--srcapp.js ///< The Client server.js ///< The Server package.json ///< Nodejs JSON
load more v
65%

Let’s begin by creating an express server that displays Hello World.,Creating an Express server,Create an index.html file which will display a Hello World message,In the app.js file above, we are requiring Express, and creating a new server. We are then requiring Socket.io.

root @socket: ~# node--versionv4 .2 .6 root @socket: ~# npm--version3 .5 .2 Copy code
load more v
75%

Run the application with node index.js and open the page in your browser.,Socket.IO allows bi-directional communication between client and server. Bi-directional communications are enabled when a client has Socket.IO in the browser, and a server has also integrated the Socket.IO package. While data can be sent in a number of forms, JSON is the simplest.,Socket.IO was created in 2010. It was developed to use open connections to facilitate realtime communication, still a relatively new phenomenon at the time.,Here is where we setup Socket.IO. It is listening for a ‘connection’ event and will run the provided function anytime this happens.

$ mkdir socket.io - example
cd socket.io - example
npm install socket.io express
load more v
40%

Online events on the business and technology topics that are defining today’s CTOs., CTO Roundtables Online events on the business and technology topics that are defining today’s CTOs. ,On the server side – you do nothing more than adding the listener shown below.,On the client side – you need to do a few more things, as per the example below.

load more v

Other "possible-undefined" queries related to "Is it possible to set up a socket.io client running (server-side) on a node.js server?"