Skip to content
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

How do I know who sent a notification? #72

Closed
Heniker opened this issue Jun 1, 2020 · 4 comments
Closed

How do I know who sent a notification? #72

Heniker opened this issue Jun 1, 2020 · 4 comments

Comments

@Heniker
Copy link

Heniker commented Jun 1, 2020

In README there is this line -

  ws.notify('openedNewsModule')

This is not useful if I can't know which client sent the message.
Is there any way of identifying the client besides using different Namespaces for each connection?

@mkozjak
Copy link
Member

mkozjak commented Jun 1, 2020

Unfortunately, no, you can only send some client id with each msg that gets sent from the client after the client was connected with a particular ID. For example:

var WebSocket = require('rpc-websockets').Client
var WebSocketServer = require('rpc-websockets').Server

// instantiate Server and start listening for requests
var server = new WebSocketServer({
  port: 8080,
  host: 'localhost'
})

const conns = {}

server.on('connection', function(s) {
    conns[s._id] = s
})

server.register('feedUpdated', function(p) {
    console.log('msg rcvd from:', p.socket_id)
})

nr = '12345678'

var ws = new WebSocket('ws://localhost:8080?socket_id=' + nr)

ws.on('open', function() {
    ws.notify('feedUpdated', { socket_id: nr })
})

I agree this should be handled internally, optionally. PRs welcome!

@mkozjak mkozjak closed this as completed Jun 1, 2020
@mkozjak mkozjak reopened this Jun 1, 2020
mkozjak added a commit that referenced this issue Jun 1, 2020
…ferences #72

Signed-off-by: Mario Kozjak <kozjakm1@gmail.com>
@mkozjak
Copy link
Member

mkozjak commented Jun 1, 2020

@Heniker, please use v5.1.2 for the above workaround.

@mkozjak mkozjak self-assigned this Jun 1, 2020
@mkozjak mkozjak closed this as completed in 5b27c68 Jun 1, 2020
@mkozjak
Copy link
Member

mkozjak commented Jun 1, 2020

@Heniker, this is now available in v5.2.0. Please test the release.

You still need to provide socket_id query parameter in your connection initialization to make any sane use of it. If you don't, a default random uuid will be generated and forwarded.

var WebSocket = require('rpc-websockets').Client
var WebSocketServer = require('rpc-websockets').Server

// instantiate Server and start listening for requests
var server = new WebSocketServer({
  port: 8080,
  host: 'localhost'
})

server.register('feedUpdated', function(params, id) {
    console.log('msg rcvd from client with id', id) // you will get '12345678' here
})

var ws = new WebSocket('ws://localhost:8080?socket_id=12345678')

ws.on('open', function() {
    ws.notify('feedUpdated')
})

@Heniker
Copy link
Author

Heniker commented Jun 2, 2020

This looks promising. Thank you for your work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants