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

Expose IP address and disconnect event #1674

Open
wants to merge 1 commit into
base: master
from

Conversation

@DRSDavidSoft
Copy link

DRSDavidSoft commented Mar 21, 2019

I'd like to be notified of when someone has connected to my session externally.

Summary

Since logConnections: true only emits a single Browser Connected: . . . line, bs has to expose the client's ip address from the socket event.

So, I am using the following code to be notified of new connections:

const nodeNotifier = require('node-notifier');
const bsUtils = require('browser-sync/dist/utils');
const emitter = browserSync.emitter;

emitter.on("client:connected", function ( data ) {
	let uaString = bsUtils.getUaString(data.ua);
	
	console.log(chalk`{green Connected:}`,
		`${uaString.name} version ${uaString.version}, IP: ${data.ip}`);

	// Additionally make a toast notification, if ( data.ip is external )
	nodeNotifier.notify({
		title: 'Browser connected',
		message: `${uaString.name} version ${uaString.version}, IP: ${data.ip}`,
		sound: false
	});

});

emitter.on("client:disconnected", function ( data ) {
	let uaString = bsUtils.getUaString(data.ua);
	
	console.log(chalk`{red Disconnected:}`,
		`${uaString.name} version ${uaString.version}`, chalk`{yellow IP:}`, `${data.ip}`);
});

Without exposing handshake.address and client.on("disconnect", { . . . }) methods, it's impossible to acquire the client's ip address and thus be notified of such events.

Scope

This PR exposes socket.io's following properties in order to write custom event listeners:

  • handshake.address
  • client.on("disconnect", { . . . })

Status

  • this PR does not seem to break any functionality.

Feedback

Would be glad to hear from you!

@DannyvanderJagt

This comment has been minimized.

Copy link

DannyvanderJagt commented Oct 21, 2019

Any progress on this?

@DRSDavidSoft

This comment has been minimized.

Copy link
Author

DRSDavidSoft commented Nov 7, 2019

@shakyShane I would appreciate it if you consider merging this! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.