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.
socket.io-client
Advanced tools
[![Build Status](https://secure.travis-ci.org/Automattic/socket.io-client.png)](http://travis-ci.org/Automattic/socket.io-client) [![NPM version](https://badge.fury.io/js/socket.io-client.png)](http://badge.fury.io/js/socket.io-client)
The socket.io-client npm package is a client-side library that enables real-time, bidirectional and event-based communication between web clients and servers. It works on every platform, browser or device, focusing equally on reliability and speed.
Connecting to a server
This code sample demonstrates how to connect to a Socket.IO server running on localhost at port 3000.
const io = require('socket.io-client');
const socket = io('http://localhost:3000');
Emitting events
This code sample shows how to emit an event to the server with an event name and associated data.
socket.emit('event_name', { some: 'data' });
Listening for events
This code sample illustrates how to listen for events emitted by the server and handle them with a callback function.
socket.on('event_name', function(data) {
console.log(data);
});
Disconnecting from the server
This code sample shows how to handle a disconnection event when the client gets disconnected from the server.
socket.on('disconnect', function() {
console.log('Disconnected from server');
});
The 'ws' package is a simple to use, blazing fast, and thoroughly tested WebSocket client and server implementation. Unlike socket.io-client, which provides an abstraction over WebSocket with additional features like auto-reconnection, 'ws' is a bare WebSocket implementation.
Faye is a set of tools for simple publish-subscribe messaging between web clients. It's similar to socket.io-client in that it provides an abstraction over WebSocket for real-time communication, but it has a different API and feature set.
SockJS-client is a browser JavaScript library that provides a WebSocket-like object. SockJS gives you a coherent, cross-browser, Javascript API which creates a low latency, full duplex, cross-domain communication channel between the browser and the web server, with WebSockets or without. It is similar to socket.io-client but focuses more on providing fallback options for environments where WebSocket is not available.
A standalone build of socket.io-client
is exposed automatically by the
socket.io server as /socket.io/socket.io.js
. Alternatively you can
serve the file socket.io.js
found at the root of this repository.
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost');
socket.on('connect', function(){
socket.on('event', function(data){});
socket.on('disconnect', function(){});
});
</script>
Socket.IO is compatible with browserify.
Add socket.io-client
to your package.json
and then:
var socket = require('socket.io-client')('http://localhost');
socket.on('connect', function(){
socket.on('event', function(data){});
socket.on('disconnect', function(){});
});
Exposed as the io
namespace in the standalone build, or the result
of calling require('socket.io-client')
.
When called, it creates a new Manager
for the given URL, and attempts
to reuse an existing Manager
for subsequent calls, unless the
multiplex
option is passed with false
.
The rest of the options are passed to the Manager
constructor (see below
for details).
A Socket
instance is returned for the namespace specified by the
pathname in the URL, defaulting to /
. For example, if the url
is
http://localhost/users
, a transport connection will be established to
http://localhost
and a Socket.IO connection will be established to
/users
.
Socket.io protocol revision number this client works with.
Reference to the Socket
constructor.
Reference to the Manager
constructor.
Reference to the Emitter
constructor.
A Manager
represents a connection to a given Socket.IO server. One or
more Socket
instances are associated with the manager. The manager
can be accessed through the io
property of each Socket
instance.
The opts
are also passed to engine.io
upon initialization of the
underlying Socket
.
Options:
reconnection
whether to reconnect automatically (true
)reconnectionDelay
how long to wait before attempting a new
reconnection (1000
)reconnectionDelayMax
maximum amount of time to wait between
reconnections (5000
). Each attempt increases the reconnection by
the amount specified by reconnectionDelay
.timeout
connection timeout before a connect_error
and connect_timeout
events are emitted (20000
)connect
. Fired upon a successful connection.connect_error
. Fired upon a connection error.
Parameters:
Object
error objectconnect_timeout
. Fired upon a connection timeout.reconnect
. Fired upon a successful reconnection.
Parameters:
Number
reconnection attempt numberreconnect_attempt
. Fired upon an attempt to reconnect.reconnecting
. Fired upon an attempt to reconnect.
Parameters:
Number
reconnection attempt numberreconnect_error
. Fired upon a reconnection attempt error.
Parameters:
Object
error objectreconnect_failed
. Fired when couldn't reconnect within reconnectionAttempts
The events above are also emitted on the individual sockets that
reconnect that depend on this Manager
.
Sets the reconnection
option, or returns it if no parameters
are passed.
Sets the reconnectionAttempts
option, or returns it if no parameters
are passed.
Sets the reconectionDelay
option, or returns it if no parameters
are passed.
Sets the reconectionDelayMax
option, or returns it if no parameters
are passed.
Sets the timeout
option, or returns it if no parameters
are passed.
connect
. Fired upon connecting.error
. Fired upon a connection error
Parameters:
Object
error datadisconnect
. Fired upon a disconnection.reconnect
. Fired upon a successful reconnection.
Parameters:
Number
reconnection attempt numberreconnect_attempt
. Fired upon an attempt to reconnect.reconnecting
. Fired upon an attempt to reconnect.
Parameters:
Number
reconnection attempt numberreconnect_error
. Fired upon a reconnection attempt error.
Parameters:
Object
error objectreconnect_failed
. Fired when couldn't reconnect within reconnectionAttempts
MIT
FAQs
Realtime application framework client
The npm package socket.io-client receives a total of 1,631,381 weekly downloads. As such, socket.io-client popularity was classified as popular.
We found that socket.io-client demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers 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.