🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

mock-socket

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
m

mock-socket

Javascript mocking library for websockets and socket.io

9.3.1
latest
100

Supply Chain Security

100

Vulnerability

100

Quality

79

Maintenance

100

License

Version published
Weekly downloads
493K
-8.54%
Maintainers
2
Weekly downloads
 
Created
Issues
74

What is mock-socket?

The mock-socket npm package is designed to mock WebSocket and Server-Sent Events (SSE) in JavaScript applications. It is particularly useful for testing and development purposes, allowing developers to simulate WebSocket connections and SSE without needing a real server.

What are mock-socket's main functionalities?

Mock WebSocket

This feature allows you to create a mock WebSocket server and client. The server listens for connections and messages, and the client can send and receive messages. This is useful for testing WebSocket interactions without needing a real server.

const { WebSocket, Server } = require('mock-socket');

const mockServer = new Server('ws://localhost:8080');

mockServer.on('connection', socket => {
  socket.on('message', data => {
    console.log('Received message from client:', data);
    socket.send('Hello from server');
  });
});

const client = new WebSocket('ws://localhost:8080');

client.onopen = () => {
  client.send('Hello from client');
};

client.onmessage = event => {
  console.log('Received message from server:', event.data);
};

Mock Server-Sent Events (SSE)

This feature allows you to create a mock Server-Sent Events (SSE) server and client. The server can send messages to the client, which can listen for these messages. This is useful for testing SSE interactions without needing a real server.

const { EventSource, Server } = require('mock-socket');

const mockServer = new Server('http://localhost:8080');

mockServer.on('connection', socket => {
  socket.send('Hello from server');
});

const eventSource = new EventSource('http://localhost:8080');

eventSource.onmessage = event => {
  console.log('Received message from server:', event.data);
};

Other packages similar to mock-socket

FAQs

Package last updated on 11 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts