Security News
RubyGems.org Adds New Maintainer Role
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.
socket.io-client
Advanced tools
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.
The socket.io
client is basically a simple HTTP Socket interface implementation.
It looks similar to WebSocket while providing additional features and
leveraging other transports when WebSocket is not supported by the user's
browser.
var socket = io.connect('http://domain.com');
socket.on('connect', function () {
// socket connected
});
socket.on('custom event', function () {
// server emitted a custom event
});
socket.on('disconnect', function () {
// socket disconnected
});
socket.send('hi there');
If you want to namespace all the messages and events emitted to a particular
endpoint, simply specify it as part of the connect
uri:
var chat = io.connect('http://localhost/chat');
chat.on('connect', function () {
// chat socket connected
});
var news = io.connect('/news'); // io.connect auto-detects host
news.on('connect', function () {
// news socket connected
});
To ease with the creation of applications, you can emit custom events outside
of the global message
event.
var socket = io.connect();
socket.emit('server custom event', { my: 'data' });
var socket = io.connect();
socket.on('connect', function () {
socket.disconnect();
});
io.connect(uri, [options]);
resource
socket.io
The resource is what allows the socket.io
server to identify incoming connections by socket.io
clients. In other words, any HTTP server can implement socket.io and still serve other normal, non-realtime HTTP requests.
transports
['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']
A list of the transports to attempt to utilize (in order of preference).
5000
The amount of milliseconds a transport has to create a connection before we consider it timed out.
true
A boolean indicating if we should try other transports when the connectTimeout occurs.
true
A boolean indicating if we should automatically reconnect if a connection is disconnected.
500
The amount of milliseconds before we try to connect to the server again. We are using a exponential back off algorithm for the following reconnections, on each reconnect attempt this value will get multiplied (500 > 1000 > 2000 > 4000 > 8000).
10
The amount of attempts should we make using the current transport to connect to the server? After this we will do one final attempt, and re-try with all enabled transport methods before we give up.
options
The passed in options combined with the defaults.
connected
Whether the socket is connected or not.
connecting
Whether the socket is connecting or not.
reconnecting
Whether we are reconnecting or not.
transport
The transport instance.
connect(λ)
Establishes a connection. If λ is supplied as argument, it will be called once the connection is established.
send(message)
A string of data to send.
disconnect
Closes the connection.
on(event, λ)
Adds a listener for the event event.
once(event, λ)
Adds a one time listener for the event event. The listener is removed after the first time the event is fired.
removeListener(event, λ)
Removes the listener λ for the event event.
connect
Fired when the connection is established and the handshake successful.
connecting(transport_type)
Fired when a connection is attempted, passing the transport name.
connect_failed
Fired when the connection timeout occurs after the last connection attempt.
This only fires if the connectTimeout
option is set.
If the tryTransportsOnConnectTimeout
option is set, this only fires once all
possible transports have been tried.
message(message)
Fired when a message arrives from the server
close
Fired when the connection is closed. Be careful with using this event, as some transports will fire it even under temporary, expected disconnections (such as XHR-Polling).
disconnect
Fired when the connection is considered disconnected.
reconnect(transport_type,reconnectionAttempts)
Fired when the connection has been re-established. This only fires if the reconnect
option is set.
reconnecting(reconnectionDelay,reconnectionAttempts)
Fired when a reconnection is attempted, passing the next delay for the next reconnection.
reconnect_failed
Fired when all reconnection attempts have failed and we where unsuccessful in reconnecting to the server.
Guillermo Rauch <guillermo@learnboost.com>
Arnout Kazemier <info@3rd-eden.com>
(The MIT License)
Copyright (c) 2010 LearnBoost <dev@learnboost.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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
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.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.