Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs Websocket chat room

const express = require('express');
const http = require('http');
const WebSocket = require('ws');

const port = 6969;
const server = http.createServer(express);
const wss = new WebSocket.Server({ server })

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(data) {
    wss.clients.forEach(function each(client) {
      if (client !== ws && client.readyState === WebSocket.OPEN) {
        client.send(data);
      }
    })
  })
})

server.listen(port, function() {
  console.log(`Server is listening on ${port}!`)
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: mongoose connecting directly rather than tunnel 
Javascript :: how to route with credentials react 
Javascript :: how to merge data react native 
Javascript :: Foreach array in JavaScript fsd 
Javascript :: btoa in js string only 
Javascript :: can not found jstl core xml file 
Javascript :: Finding the longest word in a string 
Javascript :: execute shell command from html button node js 
Javascript :: javascript const scope = await angular.element(document.body).scope(); 
Javascript :: how to make work copy paste on otp input field javascript 
Javascript :: Form Data error (unable to decode value) characters specials 
Javascript :: javascript grow function 
Javascript :: json serializable snake case 
Javascript :: jumping on the clouds hackerarnk solution in javascrit 
Javascript :: typeorm with better sqlite using query builder 
Javascript :: automatically adjust color 
Javascript :: last iteration is for loop js ES6 
Javascript :: how to print 1 to n numbers without using loop javascript 
Javascript :: js uid from 8 characters or digits 
Javascript :: Paginate array in JavaScript 
Javascript :: js date add days daylight saving 
Javascript :: svg documentation 
Javascript :: all files website checker 
Javascript :: change color jquery css 
Javascript :: react regions 
Javascript :: hide fill apexcharts 
Javascript :: suffic prefix jsps 
Javascript :: React svg element rating 
Javascript :: axios get request body 
Javascript :: javascript to prevent method POST from realoading 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =