A software engineer bent on world bovine domination.
I write code at 99designs.com and on GitHub.

By Dennis Hotson.

May 26
Permalink

Node.js chat server in CoffeeScript

I’ve been checking out CoffeeScript recently. It’s really f’kin cool.

The CoffeeScript site describes it best:

CoffeeScript is a little language that compiles into JavaScript. Think of it as JavaScript’s less ostentatious kid brother — the same genes, roughly the same height, but a different sense of style.

To kick the tires a little, I converted my old Node.js chat server to use CoffeeScript instead of just vanilla JavaScript.

Check out the code below, pretty stylish little language right?

net = require('net')

Array.prototype.remove = (element) ->
  for e, i in this when e is element
    return this.splice(i, 1)

class Client
  constructor: (stream) ->
    @stream = stream
    @name = null

clients = []

server = net.createServer((stream) ->
  client = new Client(stream)
  clients.push client

  stream.setTimeout 0
  stream.setEncoding "utf8"

  stream.addListener('connect', ->
    stream.write 'Welcome, enter your username:\n'
  )

  stream.addListener('data', (data) ->
    if client.name is null
      client.name = data.match /\S+/
      stream.write('===========\n')
      for c in clients when c isnt client
        c.stream.write(client.name + " has joined.\n")
      return

    matched = data.match /^\/(.*)/
    if matched and matched.length > 1
      command = matched[1]
      if command == 'users'
        for c in clients
          stream.write("- " + c.name + "\n")
      else if command == 'quit'
        stream.end()

      return

    for c in clients when c isnt client
      c.stream.write(client.name + ": " + data)
  )

  stream.addListener('end', ->
    clients.remove client

    for c in clients
      c.stream.write client.name+" has left.\n"

    stream.end()
  )
)

server.listen 7000


Also, as an extra bonus. I’ve written a CoffeeScript edit mode for jEdit to get syntax highlighting, auto-indent, completion etc. Get it here.