<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
	<id>https://support.qbpro.ru/index.php?action=history&amp;feed=atom&amp;title=NodeJs_-_socket.io</id>
	<title>NodeJs - socket.io - История изменений</title>
	<link rel="self" type="application/atom+xml" href="https://support.qbpro.ru/index.php?action=history&amp;feed=atom&amp;title=NodeJs_-_socket.io"/>
	<link rel="alternate" type="text/html" href="https://support.qbpro.ru/index.php?title=NodeJs_-_socket.io&amp;action=history"/>
	<updated>2026-05-14T04:57:19Z</updated>
	<subtitle>История изменений этой страницы в вики</subtitle>
	<generator>MediaWiki 1.38.1</generator>
	<entry>
		<id>https://support.qbpro.ru/index.php?title=NodeJs_-_socket.io&amp;diff=4219&amp;oldid=prev</id>
		<title>Vix: Новая страница: «Socket.IO is a Node.JS project that makes WebSockets and realtime possible in all browsers. It also enhances WebSockets by providing built-in multiplexing, horizontal scalability, automatic JSON encoding/decoding, and more.  ==Установка==  npm install socket.io  ==Использование==  First, require socket.io:   var io = require('socket.io');  Next, attach it to a HTTP/HTTPS server. If you're using the fantastic express web framework:  Expr...»</title>
		<link rel="alternate" type="text/html" href="https://support.qbpro.ru/index.php?title=NodeJs_-_socket.io&amp;diff=4219&amp;oldid=prev"/>
		<updated>2024-07-24T17:07:30Z</updated>

		<summary type="html">&lt;p&gt;Новая страница: «Socket.IO is a Node.JS project that makes WebSockets and realtime possible in all browsers. It also enhances WebSockets by providing built-in multiplexing, horizontal scalability, automatic JSON encoding/decoding, and more.  ==Установка==  npm install socket.io  ==Использование==  First, require socket.io:   var io = require(&amp;#039;socket.io&amp;#039;);  Next, attach it to a HTTP/HTTPS server. If you&amp;#039;re using the fantastic express web framework:  Expr...»&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Socket.IO is a Node.JS project that makes WebSockets and realtime possible in all browsers. It also enhances WebSockets by providing built-in multiplexing, horizontal scalability, automatic JSON encoding/decoding, and more.&lt;br /&gt;
&lt;br /&gt;
==Установка==&lt;br /&gt;
 npm install socket.io&lt;br /&gt;
&lt;br /&gt;
==Использование==&lt;br /&gt;
&lt;br /&gt;
First, require socket.io:&lt;br /&gt;
&lt;br /&gt;
 var io = require('socket.io');&lt;br /&gt;
&lt;br /&gt;
Next, attach it to a HTTP/HTTPS server. If you're using the fantastic express web framework:&lt;br /&gt;
 Express 3.x&lt;br /&gt;
  var app = express()&lt;br /&gt;
   , server = require('http').createServer(app)&lt;br /&gt;
   , io = io.listen(server);&lt;br /&gt;
 &lt;br /&gt;
 server.listen(80);&lt;br /&gt;
 &lt;br /&gt;
 io.sockets.on('connection', function (socket) {&lt;br /&gt;
   socket.emit('news', { hello: 'world' });&lt;br /&gt;
   socket.on('my other event', function (data) {&lt;br /&gt;
     console.log(data);&lt;br /&gt;
   });&lt;br /&gt;
 });&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, load it from the client side code:&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;script src=&amp;quot;/socket.io/socket.io.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
   var socket = io.connect('http://localhost');&lt;br /&gt;
   socket.on('news', function (data) {&lt;br /&gt;
     console.log(data);&lt;br /&gt;
     socket.emit('my other event', { my: 'data' });&lt;br /&gt;
   });&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
 For more thorough examples, look at the examples/ directory.&lt;br /&gt;
&lt;br /&gt;
==Примеры использования==&lt;br /&gt;
Sending and receiving events.&lt;br /&gt;
&lt;br /&gt;
Socket.IO allows you to emit and receive custom events. Besides connect, message and disconnect, you can emit custom events:&lt;br /&gt;
&lt;br /&gt;
 // note, io.listen(&amp;lt;port&amp;gt;) will create a http server for you&lt;br /&gt;
 var io = require('socket.io').listen(80);&lt;br /&gt;
 &lt;br /&gt;
 io.sockets.on('connection', function (socket) {&lt;br /&gt;
   io.sockets.emit('this', { will: 'be received by everyone' });&lt;br /&gt;
 &lt;br /&gt;
   socket.on('private message', function (from, msg) {&lt;br /&gt;
     console.log('I received a private message by ', from, ' saying ', msg);&lt;br /&gt;
   });&lt;br /&gt;
 &lt;br /&gt;
   socket.on('disconnect', function () {&lt;br /&gt;
     io.sockets.emit('user disconnected');&lt;br /&gt;
   });&lt;br /&gt;
 });&lt;br /&gt;
&lt;br /&gt;
Storing data associated to a client&lt;br /&gt;
&lt;br /&gt;
Sometimes it's necessary to store data associated with a client that's necessary for the duration of the session.&lt;br /&gt;
&lt;br /&gt;
* '''Server side'''&lt;br /&gt;
&lt;br /&gt;
 var io = require('socket.io').listen(80);&lt;br /&gt;
 &lt;br /&gt;
 io.sockets.on('connection', function (socket) {&lt;br /&gt;
   socket.on('set nickname', function (name) {&lt;br /&gt;
     socket.set('nickname', name, function () { socket.emit('ready'); });&lt;br /&gt;
   });&lt;br /&gt;
 &lt;br /&gt;
   socket.on('msg', function () {&lt;br /&gt;
     socket.get('nickname', function (err, name) {&lt;br /&gt;
       console.log('Chat message by ', name);&lt;br /&gt;
     });&lt;br /&gt;
   });&lt;br /&gt;
 });&lt;br /&gt;
&lt;br /&gt;
* '''Client side'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
   var socket = io.connect('http://localhost');&lt;br /&gt;
 &lt;br /&gt;
   socket.on('connect', function () {&lt;br /&gt;
     socket.emit('set nickname', prompt('What is your nickname?'));&lt;br /&gt;
     socket.on('ready', function () {&lt;br /&gt;
       console.log('Connected !');&lt;br /&gt;
       socket.emit('msg', prompt('What is your message?'));&lt;br /&gt;
     });&lt;br /&gt;
   });&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Restricting yourself to a namespace&lt;br /&gt;
&lt;br /&gt;
If you have control over all the messages and events emitted for a particular application, using the default / namespace works.&lt;br /&gt;
&lt;br /&gt;
If you want to leverage 3rd-party code, or produce code to share with others, socket.io provides a way of namespacing a socket.&lt;br /&gt;
&lt;br /&gt;
This has the benefit of multiplexing a single connection. Instead of socket.io using two WebSocket connections, it'll use one.&lt;br /&gt;
&lt;br /&gt;
The following example defines a socket that listens on '/chat' and one for '/news':&lt;br /&gt;
&lt;br /&gt;
* '''Server side'''&lt;br /&gt;
&lt;br /&gt;
 var io = require('socket.io').listen(80);&lt;br /&gt;
 &lt;br /&gt;
 var chat = io&lt;br /&gt;
   .of('/chat')&lt;br /&gt;
   .on('connection', function (socket) {&lt;br /&gt;
     socket.emit('a message', { that: 'only', '/chat': 'will get' });&lt;br /&gt;
     chat.emit('a message', { everyone: 'in', '/chat': 'will get' });&lt;br /&gt;
   });&lt;br /&gt;
 &lt;br /&gt;
 var news = io&lt;br /&gt;
   .of('/news');&lt;br /&gt;
   .on('connection', function (socket) {&lt;br /&gt;
     socket.emit('item', { news: 'item' });&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
* '''Client side:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
   var chat = io.connect('http://localhost/chat')&lt;br /&gt;
     , news = io.connect('http://localhost/news');&lt;br /&gt;
 &lt;br /&gt;
   chat.on('connect', function () {&lt;br /&gt;
     chat.emit('hi!');&lt;br /&gt;
   });&lt;br /&gt;
 &lt;br /&gt;
   news.on('news', function () {&lt;br /&gt;
     news.emit('woot');&lt;br /&gt;
   });&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Sending volatile messages.&lt;br /&gt;
&lt;br /&gt;
Sometimes certain messages can be dropped. Let's say you have an app that shows realtime tweets for the keyword bieber.&lt;br /&gt;
&lt;br /&gt;
If a certain client is not ready to receive messages (because of network slowness or other issues, or because he's connected through long polling and is in the middle of a request-response cycle), if he doesn't receive ALL the tweets related to bieber your application won't suffer.&lt;br /&gt;
&lt;br /&gt;
In that case, you might want to send those messages as volatile messages.&lt;br /&gt;
&lt;br /&gt;
* '''Server side'''&lt;br /&gt;
&lt;br /&gt;
 var io = require('socket.io').listen(80);&lt;br /&gt;
 &lt;br /&gt;
 io.sockets.on('connection', function (socket) {&lt;br /&gt;
   var tweets = setInterval(function () {&lt;br /&gt;
     getBieberTweet(function (tweet) {&lt;br /&gt;
       socket.volatile.emit('bieber tweet', tweet);&lt;br /&gt;
     });&lt;br /&gt;
   }, 100);&lt;br /&gt;
 &lt;br /&gt;
   socket.on('disconnect', function () {&lt;br /&gt;
     clearInterval(tweets);&lt;br /&gt;
   });&lt;br /&gt;
 });&lt;br /&gt;
&lt;br /&gt;
* '''Client side'''&lt;br /&gt;
&lt;br /&gt;
In the client side, messages are received the same way whether they're volatile or not.&lt;br /&gt;
&lt;br /&gt;
Getting acknowledgements&lt;br /&gt;
&lt;br /&gt;
Sometimes, you might want to get a callback when the client confirmed the message reception.&lt;br /&gt;
&lt;br /&gt;
To do this, simply pass a function as the last parameter of .send or .emit. What's more, when you use .emit, the acknowledgement is done by you, which means you can also pass data along:&lt;br /&gt;
&lt;br /&gt;
* '''Server side'''&lt;br /&gt;
&lt;br /&gt;
 var io = require('socket.io').listen(80);&lt;br /&gt;
 &lt;br /&gt;
 io.sockets.on('connection', function (socket) {&lt;br /&gt;
   socket.on('ferret', function (name, fn) {&lt;br /&gt;
     fn('woot');&lt;br /&gt;
   });&lt;br /&gt;
 });&lt;br /&gt;
* '''Client side'''&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
   var socket = io.connect(); // TIP: .connect with no args does auto-discovery&lt;br /&gt;
   socket.on('connect', function () { // TIP: you can avoid listening on `connect` and listen on events directly too!&lt;br /&gt;
     socket.emit('ferret', 'tobi', function (data) {&lt;br /&gt;
       console.log(data); // data will be 'woot'&lt;br /&gt;
     });&lt;br /&gt;
   });&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
* '''Broadcasting messages'''&lt;br /&gt;
&lt;br /&gt;
To broadcast, simply add a broadcast flag to emit and send method calls. Broadcasting means sending a message to everyone else except for the socket that starts it.&lt;br /&gt;
&lt;br /&gt;
* '''Server side'''&lt;br /&gt;
&lt;br /&gt;
 var io = require('socket.io').listen(80);&lt;br /&gt;
 &lt;br /&gt;
 io.sockets.on('connection', function (socket) {&lt;br /&gt;
   socket.broadcast.emit('user connected');&lt;br /&gt;
   socket.broadcast.json.send({ a: 'message' });&lt;br /&gt;
 });&lt;br /&gt;
&lt;br /&gt;
* '''Rooms'''&lt;br /&gt;
&lt;br /&gt;
Sometimes you want to put certain sockets in the same room, so that it's easy to broadcast to all of them together.&lt;br /&gt;
&lt;br /&gt;
Think of this as built-in channels for sockets. Sockets join and leave rooms in each socket.&lt;br /&gt;
&lt;br /&gt;
* '''Server side'''&lt;br /&gt;
&lt;br /&gt;
 var io = require('socket.io').listen(80);&lt;br /&gt;
 &lt;br /&gt;
 io.sockets.on('connection', function (socket) {&lt;br /&gt;
   socket.join('justin bieber fans');&lt;br /&gt;
   socket.broadcast.to('justin bieber fans').emit('new fan');&lt;br /&gt;
   io.sockets.in('rammstein fans').emit('new non-fan');&lt;br /&gt;
 });&lt;br /&gt;
 Using it just as a cross-browser WebSocket&lt;br /&gt;
&lt;br /&gt;
If you just want the WebSocket semantics, you can do that too. Simply leverage send and listen on the message event:&lt;br /&gt;
&lt;br /&gt;
* '''Server side'''&lt;br /&gt;
&lt;br /&gt;
 var io = require('socket.io').listen(80);&lt;br /&gt;
 &lt;br /&gt;
 io.sockets.on('connection', function (socket) {&lt;br /&gt;
   socket.on('message', function () { });&lt;br /&gt;
   socket.on('disconnect', function () { });&lt;br /&gt;
 });&lt;br /&gt;
&lt;br /&gt;
* '''Client side'''&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
   var socket = io.connect('http://localhost/');&lt;br /&gt;
   socket.on('connect', function () {&lt;br /&gt;
     socket.send('hi');&lt;br /&gt;
 &lt;br /&gt;
     socket.on('message', function (msg) {&lt;br /&gt;
       // my msg&lt;br /&gt;
     });&lt;br /&gt;
   });&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
Changing configuration&lt;br /&gt;
&lt;br /&gt;
Configuration in socket.io is TJ-style:&lt;br /&gt;
&lt;br /&gt;
* '''Server side'''&lt;br /&gt;
&lt;br /&gt;
 var io = require('socket.io').listen(80);&lt;br /&gt;
 &lt;br /&gt;
 io.configure(function () {&lt;br /&gt;
   io.set('transports', ['websocket', 'flashsocket', 'xhr-polling']);&lt;br /&gt;
 });&lt;br /&gt;
 &lt;br /&gt;
 io.configure('development', function () {&lt;br /&gt;
   io.set('transports', ['websocket', 'xhr-polling']);&lt;br /&gt;
   io.enable('log');&lt;br /&gt;
 });&lt;/div&gt;</summary>
		<author><name>Vix</name></author>
	</entry>
</feed>