-
Notifications
You must be signed in to change notification settings - Fork 104
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
Configurable socket namespace per emit action #125
Conversation
Can you add a test case for the new functionality please? An existing socket.io test seems to be broken as well. |
Hey @hassy, Thanks for your feedback, I fixed the failing test. I guess a new test scenario should be added here: Any other tests that need to be created? |
@menzow Cool, cheers. Yeah a simple script using namespaces will do, perhaps using two different namespaces and verifying the messages that come back. The test Socket.io servers[1] will need to be updated to send something back (same value) on different namespaces, and perhaps to print the number of clients/messages in different namespaces as well to help debugging. |
Hey @hassy, Added tests for namespaces and fixed the errors. Please review it. Edit: I did add the namespace tests to the |
Cool, thanks @menzow |
// Only process emit requests; delegate the rest to the HTTP engine (or think utility) | ||
if (requestSpec.think) { | ||
return engineUtil.createThink(requestSpec, _.get(self.config, 'defaults.think', {})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to preserve the second argument to createThink
, otherwise default think settings won't be applied.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I've put it back.
@@ -118,10 +118,10 @@ SocketIoEngine.prototype.step = function (requestSpec, ee) { | |||
return engineUtil.createLoopWithCount(requestSpec.count || -1, steps); | |||
} | |||
|
|||
let f = function(context, callback) { | |||
let f = function(context, callback, socketio) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a particular reason we are passing socketio
as an extra argument here, rather than keeping it attached to context
? That function's signature should really follow that of async#waterfall
task functions since that's where f
is used. This would avoid us needing to wrap f
in preStep
as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah you're right, I overlooked that part. Please see next commit.
@menzow I added a couple of comments inline. Thank you for your work. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Processed feedback.
// Only process emit requests; delegate the rest to the HTTP engine (or think utility) | ||
if (requestSpec.think) { | ||
return engineUtil.createThink(requestSpec, _.get(self.config, 'defaults.think', {})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I've put it back.
@@ -118,10 +118,10 @@ SocketIoEngine.prototype.step = function (requestSpec, ee) { | |||
return engineUtil.createLoopWithCount(requestSpec.count || -1, steps); | |||
} | |||
|
|||
let f = function(context, callback) { | |||
let f = function(context, callback, socketio) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah you're right, I overlooked that part. Please see next commit.
…ameter from step:f() function for better compatibility with async#waterfall
if (!(requestSpec.emit && requestSpec.emit.channel)) { | ||
ee.emit('error', 'invalid arguments'); | ||
if (!(requestSpec.emit && requestSpec.emit.channel && socketio)) { | ||
return ee.emit('error', 'invalid arguments'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hassy, I added socketio to be required here either. And I return after the error emit. Continuing execution without these variables seems unnecessary / problematic.
SocketIoEngine.prototype.getSocket = function(namespace, context, cb) { | ||
context.namespaces = context.namespaces || {}; | ||
SocketIoEngine.prototype.loadContextSocket = function(namespace, context, cb) { | ||
context.sockets = context.sockets || {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed context.namespaces
to context.sockets
. Naming seemed more explanatory.
@@ -118,20 +118,21 @@ SocketIoEngine.prototype.step = function (requestSpec, ee) { | |||
return engineUtil.createLoopWithCount(requestSpec.count || -1, steps); | |||
} | |||
|
|||
let f = function(context, callback, socketio) { | |||
let f = function(context, callback) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed socketio parameter.
Socket is now taken from context.sockets[requestSpec.emit.namespace] at line 132
.
@@ -180,52 +181,55 @@ SocketIoEngine.prototype.step = function (requestSpec, ee) { | |||
}; | |||
|
|||
function preStep(context, callback){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preStep behaves as a decorator for function f
. It ensures that the socket for the emit namespace is loaded before executing f
.
Omitting it would add unnecessary complexity inside the f
function. Separation keeps everything a bit more in line with the single responsibility principle. I ensured that the invocation parameters of preStep
and f
are the same.
Cool, nice work @menzow 👍 |
Would you mind squashing all of your commits here into one? Just one last thing before I merge. |
Cool. You could've done this instead to avoid needing to create a new PR: # in your fork:
git rebase -i HEAD~6
# change "pick" to "s" in the editor for all commits but the first one, save and exit
# in the next editor window, rewrite the commit message
# then:
git push -f origin master
# that would've updated this PR directly instead |
Merged the other PR. Cheers @menzow |
Thanks for merging! Helps out my test case a lot. I'll add a new topic in the docs explaining how to use namespaces so it may help out other users. One unrelated question: |
Github keeps all commits, even the ones that have been over-written with a force-push, so it's still possible to see the original discussion, it's just marked as "outdated". |
@hassy , thanks for the info 😃. Cheers. |
This change allows socket.IO namespaces to be configured by adding a
namespace
attribute to theemit
actions.More info: #124