Skip to content

[feat] Add a 'binary' flag #3185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- [Event: 'connection'](#event-connect)
- [Flag: 'volatile'](#flag-volatile)
- [Flag: 'local'](#flag-local)
- [Flag: 'binary'](#flag-binary)
- [Class: Socket](#socket)
- [socket.id](#socketid)
- [socket.rooms](#socketrooms)
Expand All @@ -57,6 +58,7 @@
- [socket.disconnect(close)](#socketdisconnectclose)
- [Flag: 'broadcast'](#flag-broadcast)
- [Flag: 'volatile'](#flag-volatile-1)
- [Flag: 'binary'](#flag-binary-1)
- [Event: 'disconnect'](#event-disconnect)
- [Event: 'error'](#event-error)
- [Event: 'disconnecting'](#event-disconnecting)
Expand Down Expand Up @@ -470,6 +472,14 @@ Sets a modifier for a subsequent event emission that the event data may be lost
io.volatile.emit('an event', { some: 'data' }); // the clients may or may not receive it
```

#### Flag: 'binary'

Specifies whether there is binary data in the emitted data. Increases performance when specified. Can be `true` or `false`.

```js
io.binary(false).emit('an event', { some: 'data' });
```

#### Flag: 'local'

Sets a modifier for a subsequent event emission that the event data will only be _broadcast_ to the current node (when the [Redis adapter](https://github.com/socketio/socket.io-redis) is used).
Expand Down Expand Up @@ -769,6 +779,17 @@ io.on('connection', (socket) => {
});
```

#### Flag: 'binary'

Specifies whether there is binary data in the emitted data. Increases performance when specified. Can be `true` or `false`.

```js
var io = require('socket.io')();
io.on('connection', function(socket){
socket.binary(false).emit('an event', { some: 'data' }); // The data to send has no binary data
});
```

#### Event: 'disconnect'

- `reason` _(String)_ the reason of the disconnection (either client or server-side)
Expand Down
5 changes: 4 additions & 1 deletion docs/emit.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ function onConnect(socket){
// sending a message that might be dropped if the client is not ready to receive messages
socket.volatile.emit('maybe', 'do you really need it?');

// specifying whether the data to send has binary data
socket.binary(false).emit('what', 'I have no binaries!');

// sending to all clients on this node (when using multiple nodes)
io.local.emit('hi', 'my lovely babies');

// sending to all connected clients
io.emit('an event sent to all connected clients');

Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ var emitterMethods = Object.keys(Emitter.prototype).filter(function(key){
return typeof Emitter.prototype[key] === 'function';
});

emitterMethods.concat(['to', 'in', 'use', 'send', 'write', 'clients', 'compress']).forEach(function(fn){
emitterMethods.concat(['to', 'in', 'use', 'send', 'write', 'clients', 'compress', 'binary']).forEach(function(fn){
Server.prototype[fn] = function(){
return this.sockets[fn].apply(this.sockets, arguments);
};
Expand Down
19 changes: 18 additions & 1 deletion lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
var Socket = require('./socket');
var Emitter = require('events').EventEmitter;
var parser = require('socket.io-parser');
var hasBin = require('has-binary2');
var debug = require('debug')('socket.io:namespace');

/**
Expand Down Expand Up @@ -214,7 +215,10 @@ Namespace.prototype.emit = function(ev){
}
// set up packet object
var args = Array.prototype.slice.call(arguments);
var packet = { type: parser.EVENT, data: args };
var packet = {
type: (this.flags.binary !== undefined ? this.flags.binary : hasBin(args)) ? parser.BINARY_EVENT : parser.EVENT,
data: args
};

if ('function' == typeof args[args.length - 1]) {
throw new Error('Callbacks are not supported when broadcasting');
Expand Down Expand Up @@ -277,3 +281,16 @@ Namespace.prototype.compress = function(compress){
this.flags.compress = compress;
return this;
};

/**
* Sets the binary flag
*
* @param {Boolean} Encode as if it has binary data if `true`, Encode as if it doesnt have binary data if `false`
* @return {Socket} self
* @api public
*/

Namespace.prototype.binary = function (binary) {
this.flags.binary = binary;
return this;
};
18 changes: 16 additions & 2 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

var Emitter = require('events').EventEmitter;
var parser = require('socket.io-parser');
var hasBin = require('has-binary2');
var url = require('url');
var debug = require('debug')('socket.io:socket');

Expand Down Expand Up @@ -143,7 +144,7 @@ Socket.prototype.emit = function(ev){

var args = Array.prototype.slice.call(arguments);
var packet = {
type: parser.EVENT,
type: (this.flags.binary !== undefined ? this.flags.binary : hasBin(args)) ? parser.BINARY_EVENT : parser.EVENT,
data: args
};

Expand Down Expand Up @@ -380,7 +381,7 @@ Socket.prototype.ack = function(id){

self.packet({
id: id,
type: parser.ACK,
type: hasBin(args) ? parser.BINARY_ACK : parser.ACK,
data: args
});

Expand Down Expand Up @@ -495,6 +496,19 @@ Socket.prototype.compress = function(compress){
return this;
};

/**
* Sets the binary flag
*
* @param {Boolean} Encode as if it has binary data if `true`, Encode as if it doesnt have binary data if `false`
* @return {Socket} self
* @api public
*/

Socket.prototype.binary = function (binary) {
this.flags.binary = binary;
return this;
};

/**
* Dispatch incoming event to socket listeners.
*
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
"dependencies": {
"debug": "~3.1.0",
"engine.io": "~3.1.0",
"has-binary2": "~1.0.2",
"socket.io-adapter": "~1.1.0",
"socket.io-client": "2.0.4",
"socket.io-parser": "~3.1.1"
"socket.io-parser": "~3.2.0"
},
"devDependencies": {
"expect.js": "0.3.1",
Expand Down