Connection between Socket.IO and HapiJS

We have a REST server implemented with HapiJS and a Websockets server implemented using Socket.IO (they both work on the same server, but on different ports). I want to notify the Websockets server from the HapiJS server in order to send an event with some data to a specific client.

The socket server runs on port 8081, and REST on 8080.

The idea is that the client performs an action (POST request), which is recorded in the "Event History" table. This action applies to other users, so they should be notified when this happens in real time. This is why other users are listening to the connection to the web server.

How can I tell the socket server to send an event to a specific client, and this should be done from the REST server?

I thought of three ways at the moment:

  • Separate servers for sockets and recreation and communication using RabbitMQ
  • I tried to implement Socket.IO-Emitter , but this requires a Redis database (I still don't know why). When I try to connect using the emitter from the HapiJS route handler to the socket, I get:

      export function* postRefreshEvent(userId) {
        var connection = require('socket.io-emitter')({ host: '127.0.0.1', port: 8081 });
        connection.in('UserHistory').emit('refresh', userId); 
        return {statusCode: OK}
    
      }        
    
    Error: Ready check failed: Redis connection gone from end event.
    

    in RedisClient.on_info_cmd

The update is not performed on the Socket server. I just do not see the displayed log.

  1. Make a special event and use the regular socket.io client to connect from hapijs to websites and enter a new event there.

Sample GIST .

Do you understand something like this? I appreciate any help!

+4
2

EventEmitter socket.io hapi . , , :

var Hapi = require('hapi');

// Make an event emitter for managing communication
// between hapi and socket.io code

var EventEmitter = require('events');
var notifier = new EventEmitter();

// Setup API + WS server with hapi

var server = new Hapi.Server();
server.register(require('inert'), function () {});

server.connection({ port: 4000, labels: ['api'] });
server.connection({ port: 4001, labels: ['ws'] });

var apiServer = server.select('api');
var wsServer = server.select('ws');

apiServer.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {

        reply.file('index.html');
    }
});

apiServer.route({
    method: 'GET',
    path: '/action',
    handler: function (request, reply) {

        notifier.emit('action', { time: Date.now() });
        reply('ok');
    }
});

// Setup websocket stuff

var io = require('socket.io')(wsServer.listener);

io.on('connection', function (socket) {

    // Subscribe this socket to `action` events

    notifier.on('action', function (action) {
        socket.emit('action', action);
    });
});

server.start(function () {
    console.log('Server started');
});

index.html :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="http://localhost:4001/socket.io/socket.io.js"></script>
</head>
<body>
    <script>
    var socket = io('http://localhost:4001');
    socket.on('action', function (action) {
        console.log(action);
    });
    </script>
</body>
</html>

http://localhost:4000 , http://localhost:4000/action cURL (curl http://localhost:4000/action), , -:

enter image description here

+10

https://github.com/hapijs/nes, websockets hapi . - , , hapi .

+3

Source: https://habr.com/ru/post/1608800/


All Articles