Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
nodejs-websocket
Advanced tools
A nodejs module for websocket server and client
Install with npm install nodejs-websocket
or put all files in a folder called "nodejs-websocket", and:
var ws = require("nodejs-websocket")
// Scream server example: "hi" -> "HI!!!"
var server = ws.createServer(function (conn) {
console.log("New connection")
conn.on("text", function (str) {
console.log("Received "+str)
conn.sendText(str.toUpperCase()+"!!!")
})
conn.on("close", function (code, reason) {
console.log("Connection closed")
})
}).listen(8001)
Se other examples inside the folder samples
The main object, returned by require("nodejs-websocket")
.
Returns a new Server
object.
The options
is an optional object that will be handed to net.createServer() to create an ordinary socket.
If it has a property called "secure" with value true
, tls.createServer() will be used instead.
The callback
is a function which is automatically added to the "connection"
event.
Returns a new Connection
object, representing a websocket client connection
URL
is a string with the format "ws://localhost:8000/chat" (the port can be omitted)
options
is an object that will be passed to net.connect() (or tls.connect() if the protocol is "wss:").
The properties "host" and "port" will be read from the URL
callback
will be added as "connect" listener
Sets the minimum size of a pack of binary data to send in a single frame (default: 512kiB)
Set the maximum size the internal Buffer can grow (default: 2MiB) If at any time it stays bigger than this, the connection will be closed with code 1009 This is a security measure, to avoid memory attacks
The class that represents a websocket server, much like a HTTP server
Starts accepting connections on a given port
and host
.
If the host
is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).
A port
value of zero will assign a random port.
callback
will be added as an listener for the 'listening'
event.
The underlying socket, returned by net.createServer or tls.createServer
An Array with all connected clients. It's useful for broadcasting a message:
function broadcast(server, msg) {
server.connections.forEach(function (conn) {
conn.sendText(msg)
})
}
Emitted when the server has been bound after calling server.listen
Emitted when the server closes. Note that if connections exist, this event is not emitted until all connections are completely ended.
Emitted when an error occurs. The 'close' event will be called directly following this event.
Emitted when a new connection is made successfully (after the handshake have been completed). conn is an instance of Connection
The class that represents a connection, either a client-created (accepted by a nodejs ws server) or client connection. The websocket protocol has two types of data frames: text and binary. Text frames are implemented as simple send function and receive event. Binary frames are implemented as streams: when you receive binary data, you get a ReadableStream; to send binary data, you must ask for a WritableStream and write into it. The binary data will be divided into frames and be sent over the socket.
You cannot send text data while sending binary data. If you try to do so, the connection will emit an "error" event
Sends a given string to the other side. You can't send text data in the middle of a binary transmission.
callback
will be added as a listener to write operation over the socket
Asks the connection to begin transmitting binary data. Returns a WritableStream. The binary transmission will end when the WritableStream finishes (like when you call .end on it)
Sends a single chunk of binary data (like calling connection.beginBinary().end(data))
callback
will be added as a listener to write operation over the socket
Starts the closing handshake (sends a close frame)
The underlying net or tls socket
If the connection was accepted by a nodejs server, a reference to it will be saved here. null otherwise
One of these constants, representing the current state of the connection. Only an open connection can be used to send/receive data.
Stores the OutStream object returned by connection.beginBinary(). null if there is no current binary data beeing sent.
For a connection accepted by a server, it is a string representing the path to which the connection was made (example: "/chat"). null otherwise
Read only map of header names and values. Header names are lower-cased
Emitted when the connection is closed by any side
Emitted in case of error (like trying to send text data while still sending binary data)
Emitted when a text is received. str
is a string
Emitted when the beginning of binary data is received. inStream
is a ReadableStream
Emitted when the connection is fully established (after the handshake)
FAQs
Basic server&client approach to websocket (text and binary frames)
The npm package nodejs-websocket receives a total of 4,204 weekly downloads. As such, nodejs-websocket popularity was classified as popular.
We found that nodejs-websocket demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.