elpheria/rpc-websockets

How do I know who sent a notification?

Closed this issue · 4 comments

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?

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!

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

@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')
})

This looks promising. Thank you for your work!