4.75.0 • Published 1 year ago

@onehilltech/blueprint-socket.io v4.75.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

blueprint-socket.io

A Blueprint.js module for Socket.IO

npm version Dependencies

Features

  • Create Socket.IO servers on all (or selective) server connections
  • Bind connections and namespaces to computed properties for easier access
  • Uses Blueprint listeners to handle connection events

Installation

yarn add @onehilltech/blueprint-socket.io

or

npm install @onehilltech/blueprint-socket.io --save

Listening for Connections

The server listens for Socket.IO connections by extending the ConnectionListener class provided by this module, and registering the listener for the socket.io.connection event. The ConnectionListener has two methods you can override:

  • connection(name, socket) - This method is invoked when a new client connects to the server.
  • disconnect(name, socket) - This method is invoked when a client disconnects from the server.

For both methods, the name argument is the name of the connection (from app/configs/server.js); and the socket argument is the client that connected to the server.

// app/listeners/socket.io.connection/logger.js

const { ConnectionListener } = require ('@onehilltech/blueprint-socket.io');

module.exports = ConnectionListener.extend ({
  connection (name, socket) {
    console.log (`socket.io connection on ${name}: ${socket.id}`);
  },
  
  disconnect (name, socket) {
    console.log (`socket.io disconnect on ${name}: ${socket.id}`);
  }
});

Filtering Connections

The connections property on the ConnectionListener is used to filter which connections the listener will react.

// app/listeners/socket.io.connection/logger.js

const { ConnectionListener } = require ('@onehilltech/blueprint-socket.io');

module.exports = ConnectionListener.extend ({
  connections: ['insecure'],
  
  connection (name, socket) {
    console.log (`socket.io connection on ${name}: ${socket.id}`);
  },
  
  disconnect (name, socket) {
    console.log (`socket.io disconnect on ${name}: ${socket.id}`);
  }
});

Emitting Events

The easiest way to emit an event is to bind the connection/namespace to a property on a Blueprint component (e.g., controller, server, etc.) using the io factory method. You can then use the computed property to emit an event.

You can also use the computed property to emit to a room.

const { Action, Controller } = require ('@onehilltech/blueprint');
const { io } = require ('@onehilltech/blueprint-socket.io');

module.exports = Controller.extend ({
  /// Bound to the insecure Socket.IO connection.
  insecure: io (),

  /**
   * Force emit a message to all participants.
   */
  emit () {
    return Action.extend ({
      execute (req, res) {
        const { message } = req.query;

        // Emit an event to all clients on the connection.
        this.controller.insecure.emit ('chat message', message);

        res.status (200).json (true);
      }
    })
  }
});

As shown in the example above, the insecure attribute is bound to the default namespace on the insecure connection. We can the emit the event on the socket.

Emitting to a namespace

To emit to a namespace, you bind the property to a namespace using io ().of (nsp). For example, io ().of ('/chat') will bind the property to the /chat namespace.

Happy Coding!

4.71.0

1 year ago

4.75.0

1 year ago

4.67.3

1 year ago

4.74.1

1 year ago

4.67.0

1 year ago

4.66.3

1 year ago

4.66.0

1 year ago

5.0.0-alpha.6

2 years ago

4.65.19

2 years ago

5.0.0-alpha.5

2 years ago

5.0.0-alpha.4

2 years ago

5.0.0-alpha.3

2 years ago

5.0.0-alpha.2

2 years ago

5.0.0-alpha.1

2 years ago

4.65.11

2 years ago

4.63.1

2 years ago

4.65.0

2 years ago

4.64.1

2 years ago

4.60.0

2 years ago

4.61.0

2 years ago

4.61.1

2 years ago

4.60.3

2 years ago

4.59.0

2 years ago

4.58.1

2 years ago

4.56.3

2 years ago

4.59.1

2 years ago

5.0.0-alpha.0

2 years ago

4.50.3

2 years ago

4.52.0

2 years ago

4.57.0

2 years ago

4.58.0

2 years ago

4.55.0

2 years ago

4.55.1

2 years ago

4.44.3

2 years ago

4.46.0

2 years ago

4.39.1

3 years ago

4.37.2

3 years ago

4.37.0

3 years ago

4.35.0

3 years ago

4.28.20

4 years ago

4.28.18

4 years ago

4.28.4

4 years ago

4.20.0

4 years ago

4.21.0

4 years ago

4.15.0

4 years ago

4.14.1

4 years ago

4.15.1

4 years ago

4.14.0

4 years ago

4.12.0

4 years ago

4.9.0

4 years ago

4.10.0

4 years ago

4.8.0

4 years ago

4.7.0

4 years ago

2.0.0

5 years ago

1.0.0

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago