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

can you add an example about websockets? #682

Open
badpenguin opened this issue Jun 22, 2024 · 0 comments
Open

can you add an example about websockets? #682

badpenguin opened this issue Jun 22, 2024 · 0 comments

Comments

@badpenguin
Copy link

I tried this (see below) but can't get the connection event to trigger.

'use strict'
const serverlessExpress = require('@codegenie/serverless-express');
const app = require('./app');
const server = serverlessExpress.createServer(app)
const {initWebSocket} = require('./ws');
initWebSocket(server);
exports.handler = (event, context) => {
	serverlessExpress.proxy(server, event, context);
};
const WebSocket = require('ws');

function initWebSocket(server) {

	const wss = new WebSocket.Server({server});

	wss.on('connection', (ws) => {

		console.error('*** connection ****');

		ws.on('message', (message) => {
			console.error(`Received message: ${message}`);
			ws.send(`Echo: ${message}`);
		});

		ws.on('close', () => {
			console.error('user disconnected');
		});

		ws.on('error', (error) => {
			console.error('WebSocket error:', error);
		});

		ws.send('Welcome to the WebSocket server!');
	});

}

module.exports = { initWebSocket };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant