npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-socket.io-middleware

v0.0.4

Published

This middleware allows you to use the existing HTTP REST API as a WebSocket.

Downloads

272

Readme

Express Socket.io Middleware

This middleware allows you to use the existing HTTP REST API as a WebSocket.

Installation

The easiest way to install express-socket.io-middleware is with npm.

npm install express-socket.io-middleware

Alternately, download the source.

git clone https://github.com/stegano/express-socket.io-middleware.git

Example

This middleware allows you to process all rest api requests and responses implemented as websockets.

Server

...
const app = express();
const server = http.createServer(app);
const io = new Server(server, { 
  path: '/ws',
});

app
  .use(socketIoMiddleware(io, 'http://localhost:3000', 'secret!'))
  .get('/test', (_, res) => {
    res.send({message: 'Hello World'})
  });

server.listen(3000);
...

Clients

  • You can change the event name in configuration. Please check the Configuration section.

Client With Socket.io

Request through websocket and receive a response

// 1) Create and connect socket object
const socket = io({
      path: '/ws',
      transports: ['websocket']
    });

// 2) Send request using WebSocket
socket.emit('request', {
  pathanme: '/test',
  method: 'GET',
  data: {},
  params: {}
});

// 3) Receive response using WebSocket
socket.on('response', (data) => {
  console.log(data); // `{ request: {...}, response: { ..., data: 'Hello World' }} }`
});

Client With HTTP API and Socket.io

Request using REST API and receive response using WebSocket

// 1) Create and connect socket object
const socket = io({
      path: '/ws',
      transports: ['websocket']
    });

// 2) Receive auth token via WebSocket
socket.on('token', ({token}) => {
  // 3) Send REST API request with `authentication` header
  axios.get('/test', {
    headers: {
      authorization: `Bearer ${token}`
    }
  })
});


socket.on('response', (data) => {
  // 4) Receive REST API response as WebSocket
  console.log(data); // `{ request: {...}, response: { ..., data: 'Hello World' }} }`
});

Configuration

  /**
   * Send an error message to the socket
   * When an unexpected error occurs during internal processing of socketIoMiddleware.
   */
  unexpectedErrorMessage?: string
  /**
   * This setting can transform the response payload data to be sent to the socket.
   */
  transformResponsePayload?: (data: ResponsePayload) => any
  /**
   * This setting can change the socket event name.
   */
  eventNames?: {
    /**
     * When a socket is connected, it sends a JWT. This token contains authentication information
     * about the socket to connect to when making an API request.
     */
    token?: string
    /**
     * The name of the event to request with the websocket.
     */
    request?: string
    /**
     * The name of the event that will receive a response to information requested by the websocket.
     */
    response?: string
  }
  __advanced__?: {
    /**
     * Whether keepalive is enabled when communicating with the server internally
     */
    httpKeepAlive?: boolean
    /**
     * Setting up the axios library that is internally communicating with the server
     * @see https://github.com/axios/axios#request-config
     */
    axiosRequestConfig?: AxiosRequestConfig
  }

Internal Implementation

This middleware internally sends an HTTP request to the web server and sends the received response value to the connected web socket.