W
W
websiller2018-07-11 20:44:54
Node.js
websiller, 2018-07-11 20:44:54

Web sockets in node js?

Hello! How to use sockets in node js ? That is, they should be written directly inside a running server or in some other way. And what is socket.io for? There it seems that the standard work with sockets is not so low-level.
Here is an example (maybe very stupid, I just don’t know if it’s possible or not)

var http = require('http');
var url = require("url");
var WebSocketServer = require('ws').Server;

function start() {
  var server = http.createServer(function(req, res) {
    var path = url.parse(req.url).pathname;
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    
    wss = new WebSocketServer({ port: 8888 });

    wss.on('connection', function (connection) {
      connection.send('Hello World');
    });

    res.end();
  });
  server.listen(3000);
}

exports.start = start;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Shvets, 2018-07-11
@websiller

We take Google, it sends us to the wiki, where the following is written

Socket.IO is a JavaScript library for web applications and real-time communication. It consists of two parts: the client side, which runs in the browser, and the server side for node.js. Both components have a similar API. Like node.js, Socket.IO is event-driven.
Socket.IO primarily uses the WebSocket protocol, but uses other methods as needed, such as Adobe Flash sockets, JSONP requests, or AJAX requests[2], to provide the same interface. In addition to being a WebSocket wrapper, Socket.IO contains many other features, including broadcasting to multiple sockets, storing data associated with each client, and asynchronous I/O
Can be installed via npm (node ​​package manager )

And what is "right inside a running server"?
A node.js application is a process that can listen for network connections if it needs to.
It may listen to http and websocket, it may not listen. It is possible to organize an exchange through sockets in another application, it is possible in the same one. As you wish.

D
de1m, 2018-07-12
@de1m

I would advise you to still use socket.io, it can not only websocket, but also GET and POST for clients that cannot websocket.
In your version, the bad thing is that you need two ports, and as I understand it, there will be four if you add https.
Here is an example with socket.io

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var mkdirp = require('mkdirp');

var appPort = process.env.APPPORT || "5000"

app.set('views', __dirname + '/app/views')
app.set('view engine', 'pug')
app.use(express.static(__dirname + '/public'))

server.listen(appPort, function () {
  console.log('Server listening at port %d', appPort);
});

//App components
var routes = require('./app/routes');

app.use('/', routes);

io.attach(server);

var socket = require('./app/socket');
socket.start(io);

The app/socket folder contains the rest, more details can be found here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question